How can I create MySQL table that contain two columns? [closed]

First you are gonna have to connect to the database, and then you can excecute queries:

 System.out.println("Table Creation Example!");
 Connection con = null;
 String url = "jdbc:mysql://localhost:3306/";
 String dbName = "jdbctutorial";
 String driverName = "com.mysql.jdbc.Driver";
 String userName = "root";
 String password = "root";
 String column1 = "nameOfTheFirstColumn";
 String column2 = "nameOfTheSecondColumn";

 try{
  Class.forName(driverName).newInstance();
 con = DriverManager.getConnection(url+dbName, userName, password);
 try{
  Statement st = con.createStatement();
  String table = 
  "CREATE TABLE Employee11(column1 + " varchar(10), + column2 + "Emp_name varchar(10))";
  st.executeUpdate(table);
  System.out.println("Table creation process successfully!");
  }
  catch(SQLException s){
  System.out.println("Table all ready exists!");
  }
  con.close();
  }
  catch (Exception e){
  e.printStackTrace();
  }

Adapted from this source.

Leave a Comment