记录未使用Java mysql添加到数据库中

时间:2019-01-09 15:05:32

标签: java

我正在尝试将数据添加到数据库中,但未添加记录  到数据库。一切正常,可以很好地连接和查询数据库。我不知道为什么要在下面附加代码

public void makeConnection()
 {

    try{
    Class.forName("com.mysql.jdbc.Driver");   
    Connection con1; con1=DriverManager.getConnection("jdbc:mysql://localhost/javasales","root","");         
        String query = " insert into sales_product (product, price)"
        + " values (?, ?)";

      // create the mysql insert preparedstatement
      PreparedStatement preparedStmt = (PreparedStatement) con1.prepareStatement(query);
      preparedStmt.setString (1, txtproduct.getText());
      preparedStmt.setString (2, txtprice.getText());


      // execute the preparedstatement
      preparedStmt.execute();

    }catch(ClassNotFoundException coe)
      {
        System.out.println("odbc driver not found");
         }
        catch(SQLException sqe)
      {
        System.out.println(sqe);
      }

 }

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您没有在数据库上设置自动提交,

您应该添加Connection.commit()来提交更改