Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. public class CreateTableJdbc {
  7.     static final String DATABASE_URL = "jdbc:mysql://localhost/PROSELYTE_JDBC_DB";
  8.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  9.  
  10.     static final String USER = "ВАШЕ_ИМЯ_ПОЛЬЗОВАТЕЛЯ";
  11.     static final String PASSWORD = "ВАШ_ПАРОЛЬ";
  12.  
  13.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  14.         Connection connection = null;
  15.         Statement statement = null;
  16.         try {
  17.             System.out.println("Registering JDBC driver...");
  18.             Class.forName(JDBC_DRIVER);
  19.  
  20.             System.out.println("Creating connection to database...");
  21.             connection = DriverManager.getConnection(DATABASE_URL, USER, PASSWORD);
  22.  
  23.             System.out.println("Creating table in selected database...");
  24.             statement = connection.createStatement();
  25.  
  26.             String SQL = "CREATE TABLE developers " +
  27.                     "(id INTEGER not NULL, " +
  28.                     " name VARCHAR(50), " +
  29.                     " specialty VARCHAR (50), " +
  30.                     " salary INTEGER not NULL, " +
  31.                     " PRIMARY KEY (id))";
  32.  
  33.             statement.executeUpdate(SQL);
  34.             System.out.println("Table successfully created...");
  35.         }finally {
  36.             if(statement!=null){
  37.                 statement.close();
  38.             }
  39.             if(connection!=null){
  40.                 connection.close();
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement