Decide whether to build a table by checking
If the table has not been established, execute the PTMT statement update and create the table in the database.
If the table already exists, do not perform the table operation;
Package Sqlcon;import java.sql.*;p ublic class Createtables {public static Boolean havetable (String TableName) throws Clas Snotfoundexception, SQLException {class.forname ("com.mysql.jdbc.Driver"); String user = "root"; String pwd = "123456"; String url = "Jdbc:mysql://localhost:3306/mydb?useunicode=true&characterencoding=utf-8&usessl=false"; Connection conn = drivermanager.getconnection (URL, user, pwd); ResultSet rs = Conn.getmetadata (). gettables (null, NULL, TableName, NULL); if (Rs.next ()) {rs.close (); Conn.close (); return true; }else {rs.close (); Conn.close (); return false; }}public static void Main (String args[]) throws SQLException, ClassNotFoundException {class.forname (" Com.mysql.jdbc.Driver "); String user = "root"; String pwd = "123456"; String url = "Jdbc:mysql://localhost:3306/activiti?useunicode=true&characterencoding=utf-8&usessl=false"; Connection conn = drivermanager.getconnection (URL, user, pwd); if (havetable ("Mytables")!=true) {String sql = "CREATE TABLE act_ship_user" + "(id_ varchar () not NULL," + "org_ varchar (25 5), "+" job_ VARCHAR (255), "+" PRIMARY KEY (id_)) "; PreparedStatement ptmt = conn.preparestatement (sql);p tmt.executeupdate (SQL);p tmt.close (); else {System.out.println ("table is existed!"); }}}
JDBC MySQL dynamic build table