Advertisement
Guest User

HB CONNECT

a guest
Jun 5th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package zarovizsgam;
  2.  
  3.  
  4.  
  5. import java.sql.*;
  6.  
  7. public class Tabla_letrehozas {
  8.    // JDBC driver name and database URL
  9.    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  10.    static final String DB_URL = "jdbc:mysql://localhost/TanuloNyilvantarto2";
  11.  
  12.    //  Database credentials
  13.    static final String USER = "root";
  14.    static final String PASS = "";
  15.    
  16.    public static void main(String[] args) {
  17.    Connection conn = null;
  18.    Statement stmt = null;
  19.    try{
  20.       //STEP 2: Register JDBC driver
  21.       Class.forName("com.mysql.jdbc.Driver");
  22.  
  23.       //STEP 3: Open a connection
  24.       System.out.println("Connecting to a selected database...");
  25.       conn = DriverManager.getConnection(DB_URL, USER, PASS);
  26.       System.out.println("Connected database successfully...");
  27.      
  28.       //STEP 4: Execute a query
  29.       System.out.println("Creating table in given database...");
  30.       stmt = conn.createStatement();
  31.      
  32.       String sql = "CREATE TABLE `VT`.`tagok` ( `azonosito` INT(255) NULL DEFAULT NULL , `vezeteknev` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL , `keresztnev` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL , `beosztas` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL , `lakhely` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL , `cim` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL , `megjegyzes` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_hungarian_ci NOT NULL ) ENGINE = MyISAM;";
  33.  
  34.       stmt.executeUpdate(sql);
  35.       System.out.println("Created table in given database...");
  36.    }catch(SQLException se){
  37.       //Handle errors for JDBC
  38.       se.printStackTrace();
  39.    }catch(Exception e){
  40.       //Handle errors for Class.forName
  41.       e.printStackTrace();
  42.    }finally{
  43.       //finally block used to close resources
  44.       try{
  45.          if(stmt!=null)
  46.             conn.close();
  47.       }catch(SQLException se){
  48.       }// do nothing
  49.       try{
  50.          if(conn!=null)
  51.             conn.close();
  52.       }catch(SQLException se){
  53.          se.printStackTrace();
  54.       }//end finally try
  55.    }//end try
  56.    System.out.println("Goodbye!");
  57. }//end main
  58. }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement