java · jdbc · mysql

SqlExecuter vs JDBC

public class CmdExec { public static void main(String argv[]) { executeSql(); } private static void executeSql() { final class SqlExecuter extends SQLExec { public SqlExecuter() { Project project = new Project(); project.init(); setProject(project); // setTaskType(“sql”); setTaskName(“sql”); } } SqlExecuter executer = new SqlExecuter(); executer.setSrc(new File(“/home/admin/Obama/scripfile.sql”)); executer.setDriver(“com.mysql.jdbc.Driver”); executer.setPassword(“root”); executer.setUserid(“root”); executer.setUrl(“jdbc:mysql://localhost:3306/Central_Share”); executer.execute(); } } //add dependency <dependency>… Continue reading SqlExecuter vs JDBC

java · jdbc · mysql

how to get last insert id by java

public static int getLastInsertID(Connection conn, String statment)throws SQLException { int lastInsertedId = -1; PreparedStatement ps = conn.prepareStatement(statment,PreparedStatement.RETURN_GENERATED_KEYS); ps.executeUpdate(); ResultSet rskey = ps.getGeneratedKeys(); if (rskey != null && rskey.next()) { lastInsertedId = rskey.getInt(1); } return lastInsertedId; }