JDBC MySQL dynamic build table

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.