首先我们先介绍一下我们连接的具体的要求:
在项目工程目录下新建一个config文件夹,在config文件夹里创建一个database.properties文件,配置相关Oracle数据库的driver、url、username、password。
Java代码
1. #Oracle
2. jdbc.driver=oracle.jdbc.driver.OracleDriver
3. jdbc.url=jdbc:oracle:thin:@localhost:1521:moonlight
4. jdbc.username=scott
5. jdbc.password=tiger
#Oracle
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:moonlight
jdbc.username=scott
jdbc.password=tiger
在DBUtil.java中封装数据库常用操作。
Java代码
1. import java.io.FileInputStream;
2. import java.sql.Connection;
3. import java.sql.DriverManager;
4. import java.sql.PreparedStatement;
5. import java.sql.ResultSet;
6. import java.sql.SQLException;
7. import java.util.Properties;
8.
9. public class DBUtil {
10. public static Connection getConnection() {
11. Connection conn = null;
12. Properties props = null;
13. FileInputStream in = null;
14.
15. try {
16. props = new Properties();
17. in = new FileInputStream("config/database.properties");
18. props.load(in);
19. in.close();
20.
21. String jdbcdriver = props.getProperty("jdbc.driver");
22. if (jdbcdriver != null) {
23. System.setProperty("jdbc.driver", jdbcdriver);
24. }
25.
26. String url = props.getProperty("jdbc.url");
27. String username = props.getProperty("jdbc.username");
28. String password = props.getProperty("jdbc.password");
29.
30. conn = DriverManager.getConnection(url, username, password);
31. } catch (Exception e) {
32. e.printStackTrace();
33. }
34.
35. return conn;
36. }
37.
38. public static void close(Connection conn) {
39. if (conn != null) {
40. try {
41. conn.close();
42. } catch (SQLException e) {
43. e.printStackTrace();
44. }
45. }
46. }
47.
48. public static void close(PreparedStatement pstmt) {
49. if (pstmt != null) {
50. try {
51. pstmt.close();
52. } catch (SQLException e) {
53. e.printStackTrace();
54. }
55. }
56. }
57.
58. public static void close(ResultSet rs) {
59. if (rs != null) {
60. try {
61. rs.close();
62. } catch (SQLException e) {
63. e.printStackTrace();
64. }
65. }
66. }
67. }
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
public static Connection getConnection() {
Connection conn = null;
Properties props = null;
FileInputStream in = null;
try {
props = new Properties();
in = new FileInputStream("config/database.properties");
props.load(in);
in.close();
String jdbcdriver = props.getProperty("jdbc.driver");
if (jdbcdriver != null) {
System.setProperty("jdbc.driver", jdbcdriver);
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
接下来:
Java代码
1. import java.sql.Connection;
2. import java.sql.PreparedStatement;
3. import java.sql.ResultSet;
4.
5. public class JDBCDemo {
6. public static void main(String[] args) throws Exception {
7. Connection conn = null;
8. PreparedStatement pstmt = null;
9. ResultSet rs = null;
10.
11. String keyword = "SMI";
12.
13. String sql = "SELECT empno, ename, job FROM emp WHERE ename LIKE ?";
14.
15. conn = DBUtil.getConnection();
16. pstmt = conn.prepareStatement(sql);
17.
18. pstmt.setString(1, "%" + keyword + "%");
19.
20. rs = pstmt.executeQuery();
21. while (rs.next()) {
22. int empno = rs.getInt("empno");
23. String ename = rs.getString("ename");
24. String job = rs.getString("job");
25.
26. System.out.println("empno: " + empno + ", ename: " + ename
27. + ", job: " + job);
28. }
29.
30. DBUtil.close(rs);
31. DBUtil.close(pstmt);
32. DBUtil.close(conn);
33. }
34. }
这一步是为了检验import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String keyword = "SMI";
String sql = "SELECT empno, ename, job FROM emp WHERE ename LIKE ?";
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyword + "%");
rs = pstmt.executeQuery();
while (rs.next()) {
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
System.out.println("empno: " + empno + ", ename: " + ename
+ ", job: " + job);
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
}
}
这一步是为了检验import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String keyword = "SMI";
String sql = "SELECT empno, ename, job FROM emp WHERE ename LIKE ?";
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyword + "%");
rs = pstmt.executeQuery();
while (rs.next()) {
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
System.out.println("empno: " + empno + ", ename: " + ename
+ ", job: " + job);
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
}
}
这一步是为了检验在JDBCDemo.java中测试一下是否配置连接成功。
阅读(2431) | 评论(0) | 转发(0) |