修改数据
package com.javakc.test1;
import com.javakc.db.Utils;
import com.javakc.vo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class testUpdate {
public static void update(User u) throws SQLException {
Connection connection = null;
try {
connection = Utils.getConnection();
String sql = "update user set name=? where id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, u.getName());
preparedStatement.setInt(2, u.getId());
int rows = preparedStatement.executeUpdate();
System.out.println(rows);
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws SQLException {
User u = new User();
u.setId(3);
u.setName("3");
update(u);
}
}