Aaron
Aaron

Reputation: 11673

JDBC Java Prepared Statement execution error

Why won't the preparedstatement insert the data into the database table?

import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost/";
  String dbName = "java";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "";
  PreparedStatement pst;

  try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      conn.setAutoCommit(false);

      String sql = "INSERT INTO test (url) values(?)";
      PreparedStatement statement = conn.prepareStatement(sql);

      statement.setString(1, "teast.com");
      statement.executeUpdate();


      statement.close();




  } 
  catch (Exception e) {
      e.printStackTrace();
  }
 }
}

Upvotes: 1

Views: 1432

Answers (1)

skaffman
skaffman

Reputation: 403441

Because you explicitly disabled autocommit:

conn.setAutoCommit(false);

By default, JDBC will auto-commit any transactions that are implicitly created.

With auto-commit disabled, you need to explicitly commit, e.g.

statement.executeUpdate();
conn.commit();

Upvotes: 9

Related Questions