本例以JDBC 和 MY SQL DataBase为基础,来演示执行SQL来更新数据,使用Statement和Prepared Statement两种方式。

一,示例代码

连接数据库uilt: MySqlJDBSUtil.java
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
package com.devnp.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySqlJDBSUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8";
private static String userName = "root"; // base on your login user name
private static String password = "!qaz2wsx"; // base on your login user

public static Connection getConnection() {

Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static void connectionClose(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

更新数据代码:
JDBCUpdateRecord.java

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
76
77
78
79
80
81
82
83
84
85
86
package com.devnp.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUpdateRecord {

public static void main(String[] args) throws SQLException {
updateDbTableWithStatement();
updateDbTableWithPreparedStatement();
}

public static void updateDbTableWithStatement() throws SQLException {
Connection dbConnection = null;
Statement statement = null;

String updateTableSQL = "UPDATE USER_INFO SET USERNAME = 'duliu' "
+ " WHERE USER_ID = 1";

try {
dbConnection = MySqlJDBSUtil.getConnection();
statement = dbConnection.createStatement();

System.out.println(updateTableSQL);

// execute update SQL stetement
statement.execute(updateTableSQL);

System.out.println("Updated USER_INFO table record is Success!");

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (statement != null) {
statement.close();
}

if (dbConnection != null) {
dbConnection.close();
}

}

}

public static void updateDbTableWithPreparedStatement() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;

String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? "
+ " WHERE USER_ID = ?";

try {
dbConnection = MySqlJDBSUtil.getConnection();
preparedStatement = dbConnection.prepareStatement(updateTableSQL);

preparedStatement.setString(1, "duliu");
preparedStatement.setInt(2, 2);

// execute update SQL stetement
preparedStatement.executeUpdate();

System.out.println("Updated USER_INFO table record is Success!");

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (preparedStatement != null) {
preparedStatement.close();
}

if (dbConnection != null) {
dbConnection.close();
}

}
}
}

二,测试

测试结果:
UPDATE USER_INFO SET USERNAME = 'duliu' WHERE USER_ID = 1
 Updated USER_INFO table record is Success!
 Updated USER_INFO table record is Success!
数据库更新: