-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcDemo06.java
More file actions
75 lines (67 loc) · 2.21 KB
/
Copy pathJdbcDemo06.java
File metadata and controls
75 lines (67 loc) · 2.21 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
/**
* @author : CodeWater
* @create :2022-03-09-10:30
* @Function Description :执行DDL语句
*/
public class JdbcDemo06 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
String sql = "select * from account";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rs.next();
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id + "----" + name + "-----" + balance + "----");
//游标向下移动一行
rs.next();
int id2 = rs.getInt(1);
String name2 = rs.getString("name");
double balance2 = rs.getDouble(3);
System.out.println(id2 + "----" + name2 + "----" + balance2 + "----");
rs.next();
int id3 = rs.getInt(1);
String name3 = rs.getString("name");
double balance3 = rs.getDouble(3);
System.out.println(id3 + "----" + name3 + "----" + balance3 + "----");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}