Advertisement
nguyenvanquan7826

Connect database with Java and MySQL - part 1

May 5th, 2014
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. package nguyenvanquan7826.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class MyConnect {
  8.  
  9.     private final String className = "com.mysql.jdbc.Driver";
  10.     private final String url = "jdbc:mysql://localhost:3306/student";
  11.     private final String user ="root";
  12.     private final String pass ="lamgicopass";
  13.  
  14.     private Connection connection;
  15.  
  16.     private void connect() {
  17.         try {
  18.             Class.forName(className);
  19.             connection = DriverManager.getConnection(url, user, pass);
  20.             System.out.println("Connect success!");
  21.         } catch (ClassNotFoundException e) {
  22.             System.out.println("Class not found!");
  23.         } catch (SQLException e) {
  24.             System.out.println("Error connection!");
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args) {
  29.         MyConnect myConnect = new MyConnect();
  30.         myConnect.connect();
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement