gpt4 book ai didi

java - 如何使用java将值插入sql server?

转载 作者:行者123 更新时间:2023-12-02 06:23:52 25 4
gpt4 key购买 nike

抱歉,如果我的问题相同。我试图找出在线文档,但仍然没有解决问题。这是“stmt.executeUpdate ();”行中的错误

public class DBConnect {
private List<SinhVien> result = new ArrayList<SinhVien>();
public void updateSQL(String masv, String malop, String ten,
Date ngaysinh, String diachi) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection("jdbc:sqlserver://MyPC:1433;databasename=QLSV;username=sa;password=APASSWORD");
PreparedStatement stmt = connection.prepareStatement("INSERT INTO SinhVien(masv, malop, ten, ngaysinh, diachi) VALUES(?, ?, ?, ?, ?");
stmt.setString(1, masv);
stmt.setString(2, malop);
stmt.setString(3, ten);
stmt.setDate(4, ngaysinh);
stmt.setString(5, diachi);
stmt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
DBConnect dbConnect = new DBConnect();
dbConnect.XuatDSSV();
dbConnect.findSinhvienById("51003146");
dbConnect.updateSQL("123", "malop", "ten", null, "diachi");

}

最佳答案

我建议您将查询存储在字符串中。这样会更容易阅读并找到问题......

final String query = "INSERT INTO SinhVien"
+ "(masv, malop, ten, ngaysinh, diachi) "
+ "VALUES(?, ?, ?, ?, ?)"; // <-- You were missing the close paren.
PreparedStatement stmt = connection.prepareStatement(query);

关于java - 如何使用java将值插入sql server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com