我在更新数据库时遇到问题。它不会更新我编辑的任何字段。请帮忙 我的代码如下
package model;
import java.sql.*;
public class Datahandler {
public ResultSet databaseResult(Connection c,String query) throws SQLException{
Statement stmt=c.createStatement();
ResultSet rs=stmt.executeQuery(query);
return rs;
}
public void databaseDelete(Connection c,String query) throws SQLException{
Statement stmt=c.createStatement();
stmt.executeUpdate(query);
}
public int databaseInsert(Connection c,Contactdb pb) throws SQLException{
int rowsaffected=0;
try{
String query="INSERT INTO contactDb (first_name,last_name,phone_number,company_name,state,city,street,building_no) VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement statement = c.prepareStatement(query);
statement.setString(1, pb.get_firstname());
statement.setString(2,pb.get_lastname());
statement.setString(3, pb.get_phonenumber());
statement.setString(4, pb.get_companyname());
statement.setString(5, pb.get_state());
statement.setString(6, pb.get_city());
statement.setString(7, pb.get_street());
statement.setInt(8,pb.get_buildingno());
statement.execute();
} catch(SQLException e){
e.printStackTrace();
} finally {
c.close();
}
return rowsaffected;
}
public int databaseUpdate(Connection c,Contactdb pb) throws SQLException{
int a=0;
try{
String query="UPDATE contactDb SET first_name=?,last_name=?,phone_number=?,company_name=?,state=?,city=?,street=?,building_no=? WHERE sn=?";
a=69;
PreparedStatement statement = c.prepareStatement(query);
statement.setString(1, pb.get_firstname());
statement.setString(2,pb.get_lastname());
statement.setString(3, pb.get_phonenumber());
statement.setString(4, pb.get_companyname());
statement.setString(5, pb.get_state());
statement.setString(6, pb.get_city());
statement.setString(7, pb.get_street());
statement.setInt(8,pb.get_buildingno());
statement.setInt(9,pb.get_sn());
} catch(SQLException e){
e.printStackTrace();
} finally{
c.close();
}
return a;
}
}
我在更新数据库时遇到问题。它不会更新我编辑的任何字段。请帮助
答案 0 :(得分:1)
方法databaseUpdate()似乎缺少将执行update语句的statement.executeUpdate()。
答案 1 :(得分:0)
在设置表的所有字段后,您忘记执行数据库更新语句。
public int databaseUpdate(Connection c,Contactdb pb) throws SQLException{
int a=0;
try{
String query="UPDATE contactDb SET first_name=?,last_name=?,phone_number=?,company_name=?,state=?,city=?,street=?,building_no=? WHERE sn=?";
a=69;
PreparedStatement statement = c.prepareStatement(query);
statement.setString(1, pb.get_firstname());
statement.setString(2,pb.get_lastname());
statement.setString(3, pb.get_phonenumber());
statement.setString(4, pb.get_companyname());
statement.setString(5, pb.get_state());
statement.setString(6, pb.get_city());
statement.setString(7, pb.get_street());
statement.setInt(8,pb.get_buildingno());
statement.setInt(9,pb.get_sn());
*statement.executeUpdate();*
}
catch(SQLException e){
e.printStackTrace();
}
finally{
c.close();
}
return a;
}
}