Reputation: 11673
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
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