1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.SQLException;
4
5public class ConnDB {
6
7 Connection conn;
8
9
10 public Connection GetConn() {
11 try {
12 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
13 } catch (InstantiationException e) {
14 // TODO Auto-generated catch block
15 e.printStackTrace();
16 } catch (IllegalAccessException e) {
17 // TODO Auto-generated catch block
18 e.printStackTrace();
19 } catch (ClassNotFoundException e) {
20 // TODO Auto-generated catch block
21 e.printStackTrace();
22 }
23 String url = "jdbc:oracle:thin:@localhost:1521:oracled";// 数据库连接url,oracled对应你的数据库名
24 String UserName = "test";// 数据库用户登陆名(也有说是schema名字的)
25 String Password = "test";// 密码
26 try {
27 conn = DriverManager.getConnection(url, UserName, Password);
28 } catch (SQLException e) {
29 // TODO Auto-generated catch block
30 e.printStackTrace();
31 }
32 return conn;
33 }
34
35
36 public static void main(String[] args) {
37 ConnDB connDB = new ConnDB();
38 System.out.println(connDB.GetConn());
39 }
40}
41
42
2import java.sql.DriverManager;
3import java.sql.SQLException;
4
5public class ConnDB {
6
7 Connection conn;
8
9
10 public Connection GetConn() {
11 try {
12 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
13 } catch (InstantiationException e) {
14 // TODO Auto-generated catch block
15 e.printStackTrace();
16 } catch (IllegalAccessException e) {
17 // TODO Auto-generated catch block
18 e.printStackTrace();
19 } catch (ClassNotFoundException e) {
20 // TODO Auto-generated catch block
21 e.printStackTrace();
22 }
23 String url = "jdbc:oracle:thin:@localhost:1521:oracled";// 数据库连接url,oracled对应你的数据库名
24 String UserName = "test";// 数据库用户登陆名(也有说是schema名字的)
25 String Password = "test";// 密码
26 try {
27 conn = DriverManager.getConnection(url, UserName, Password);
28 } catch (SQLException e) {
29 // TODO Auto-generated catch block
30 e.printStackTrace();
31 }
32 return conn;
33 }
34
35
36 public static void main(String[] args) {
37 ConnDB connDB = new ConnDB();
38 System.out.println(connDB.GetConn());
39 }
40}
41
42