新增数据
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 testInsert {
public static void insert(User u) throws SQLException {
Connection connection = null;
try {
connection = Utils.getConnection();
String sql = "insert into user(id,name)values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, u.getId());
preparedStatement.setString(2, u.getName());
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(5);
u.setName("5");
insert(u);
}
}