0%

JDBC连接数据库

前言:复习一下java连接数据库‘最原始’的方法

步骤

  1. 加载JDBC驱动
  2. 建立并获取数据库连接
  3. 创建 JDBC Statements 对象
  4. 设置SQL语句的传入参数
  5. 执行SQL语句并获得查询结果
  6. 对查询结果进行转换处理并将处理结果返回
  7. 释放相关资源(关闭Connection,关闭Statement,关闭ResultSet)

代码

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
52
53
54
55
56
57
58
59
60
61
62
63
64
public static List<Map<String,Object>> queryForList(){  
Connection connection = null;
ResultSet rs = null;
PreparedStatement stmt = null;
List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();

try {
// 加载JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:ORACLEDB";

String user = "trainer";
String password = "trainer";

// 获取数据库连接
connection = DriverManager.getConnection(url,user,password);

String sql = "select * from userinfo where user_id = ? ";
// 创建Statement对象(每一个Statement为一次数据库执行请求)
stmt = connection.prepareStatement(sql);

// 设置传入参数
stmt.setString(1, "zhangsan");

// 执行SQL语句
rs = stmt.executeQuery();

// 处理查询结果(将查询结果转换成List<Map>格式)
ResultSetMetaData rsmd = rs.getMetaData();
int num = rsmd.getColumnCount();

while(rs.next()){
Map map = new HashMap();
for(int i = 0;i < num;i++){
String columnName = rsmd.getColumnName(i+1);
map.put(columnName,rs.getString(columnName));
}
resultList.add(map);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭结果集
if (rs != null) {
rs.close();
rs = null;
}
// 关闭执行
if (stmt != null) {
stmt.close();
stmt = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return resultList;
}
-------------本文结束感谢您的阅读-------------