-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.java
More file actions
51 lines (49 loc) · 1.52 KB
/
Copy pathService.java
File metadata and controls
51 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Win_SQL;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
//username:null
//passwd:null
//db_name:null
public class Service {
//获取连接器
public static Connection getConnection() throws IOException, SQLException{
//从config.properties文件中读取配置信息
Properties prop=new Properties();
File file=new File("./config.properties");
FileInputStream fis=new FileInputStream(file);
prop.load(fis);
String host=prop.getProperty("host");
String driver=prop.getProperty("driver");
String username=prop.getProperty("username");
String passwd=prop.getProperty("passwd");
String db_name=prop.getProperty("db_name");
String url=null;
if (driver=="oracle.jdbc.driver.Oracle"){
url="jdbc?:oracle:thin:@"+host+":1521:"+db_name;
}else if (driver=="com.mysql.jdbc.Driver"){
url="jdbc:mysql://"+host+":3306/"+db_name;
}
//加载驱动类
Connection con=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url,username,passwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Cannot find Class");
}
return con;
}
//获取statement
public static Statement createStatement() throws IOException, SQLException{
Statement state=getConnection().createStatement();
return state;
}
}