【问题标题】:Why I'm unable to update MySql table via Java Application(netbeans)?为什么我无法通过 Java Application(netbeans) 更新 MySql 表?
【发布时间】:2017-11-05 04:44:52
【问题描述】:

这是我正在使用的代码(MySql 表没有得到更新,语句 system.out.print("error"); 正在工作,并且“错误”显示在输出窗口中):

 try {

    Class.forName("java.sql.Driver");
String url = "jdbc:mysql://localhost/ghyrdkl";
Connection con = DriverManager.getConnection(url,"abcxyz","xyzabc");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from feedback;");
rs.last(); int  d = rs.getInt("id");

int idvariable = ++d; String namevariable = nametf.getText(); int starsvariable = Integer.parseInt(starsl.getText()); String wordsvariable = wordsta.getText();
String insert="INSERT INTO feedback VALUES('"+idvariable+"','"+namevariable+"'"+starsvariable+"','"+wordsvariable+"');";
       stmt.executeUpdate(insert);

    }
    catch (Exception e)
    { 
        System.out.print("error");
    }

【问题讨论】:

  • 显示System.out.print("error: " + e.getMessage());

标签: java mysql netbeans


【解决方案1】:

在您 commit 之前,更改不会显示在数据库中。

stmt.executeUpdate(insert);
con.commit(); // exception may occur, prepare to handle SQLException!

但是,这不是您遇到问题的原因。

'"+idvariable+"' 是一个字符串值。大概,idVariable 应该是一个整数。如果列不是数字类型,您将收到类型错误,这解释了错误打印消息。

如果没有抛出的实际异常,可能还有其他问题无法诊断。而不是System.out.print("error") 考虑e.printStackTrace();

【讨论】:

    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 2021-01-12
    • 2015-01-09
    • 2013-05-16
    • 2015-06-26
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多