java问题求救呀!

来源:百度知道 编辑:UC知道 时间:2024/05/05 06:42:13
我写了个程序,想向数据库中写入数据,高手们帮我看看下面的代码有错吗?很急的!

class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
String tf1 = textField1.getText();
String tf2 = textField2.getText();
String tf3 = textField3.getText();
String tf4 = textField4.getText();
String tf5 = textField5.getText();
String tf6 = textField6.getText();
String tf7 = textField7.getText();
String tf8 = textField8.getText();
String tf9 = textField9.getText();
String tf10 = textField10.getText();
String ch1 = choice1.getSelectedItem();
String ch2 = choice2.getSelectedItem();
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {

试一下在conn.close();之间也关闭smt:smt.close();
要是还不行那确定你能连得上数据库,而且插入的数据类型要和字段是统一的.可以参看我的小像子:
package SQL;
import java.sql.*;
public class ConnectionDB{ String string;
public ConnectionDB(String strsql){
string=strsql;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException ce){System.out.println(ce.getMessage());}
try{
Connection con=DriverManager.getConnection("jdbc:odbc:post","dong","");
Statement stmt=con.createStatement();
....//省略了一些
String str="Update TradeMain set Cash=Cash-'"+getCash()+"'where TradeID like'"+getTradeID()+"'";
stmt.executeUpdate(strsql);
stmt.close();
con.close();
}catch(SQLException e){System.out.println(e.getMessage());}
}
}

搂主这句错了吧smt.executeUpdate(sql);
改成smt.execute(sql);
建议楼主用下hibernateTemplate,你会感觉到前所未有的方便