我的代码是这样
JButton btn_confirm = new JButton("确定修改");
btn_confirm.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));
btn_confirm.setBounds(240, 211, 110, 39);
frm_update.getContentPane().add(btn_confirm);
btn_confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if(ttime.getText().length()>10||ttime.getText().length()<8) {
formatworng();
return;
}
if(tname.getText() == "") {
formatworng();
return;
}
if(tunit.getText() == "") {
formatworng();
return;
}
for(int i = 0 ; i < ttime.getText().length(); i++){
int n = (int)ttime.getText().charAt(i);
if(n<45||n>57) {
formatworng();
return;
}
if(i==4 && n!=45) {
formatworng();
return;
}
}
for(int i = 0 ; i < tprice.getText().length(); i++){
int n = (int)tprice.getText().charAt(i);
if(n<46||n>57) {
formatworng();
return;
}
}
for(int i = 0 ; i < tcount.getText().length(); i++){
int n = (int)tcount.getText().charAt(i);
if(n<48||n>57) {
formatworng();
return;
}
}
String str = search.getText();
conn = DriverManager.getConnection(dbURL,userName,userPwd);
Statement stmt = conn.createStatement();
String sql2 = null;
if(flag == 0 || flag == 1) {
sql2 = "update InvInfo set InvName='"+tname+"' ,InvCount="+tcount+",InvUnit='"+tunit+"',InvPrice="+tprice+"+,WarehousingTime='"+ttime+"' where InvNum = "+str;
}else if(flag == 2){
sql2 = "update InvInfo set InvName='"+tname+"' ,InvCount="+tcount+",InvUnit='"+tunit+"',InvPrice="+tprice+"+,WarehousingTime='"+ttime+"' where InvName = '"+str+"'";
}
int count = 0;
count = stmt.executeUpdate(sql2);
if(count != 0) {
UpdateSuccess();
}
抛出异常
sql2 = "update InvInfo set InvName='"+tname+"' where InvNum = "+str;
这次异常又不一样了,这咋解决
你的tname是一个控件吧,你需要的是将控件的文本值存入数据库,那么你应该是要先获取控件的文本值:tname..getText() 而不是把整个控件本身存进去
建表的时候这个字段的长度和你的输入字符串的长度是多少