Advertisement
Guest User

Untitled

a guest
May 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. public class mySQL {
  2.     public static Connection Connection = null;
  3.  
  4.     public static void createConnection() {
  5.         try {
  6.             String driverName = "org.gjt.mm.mysql.Driver";
  7.             Class.forName(driverName);
  8.             String serverName = "localhost"; // the database
  9.             String mydatabase = "database"; // the database name
  10.             String url = "jdbc:mysql://" + serverName +  "/" + mydatabase;
  11.             String username = "username"; // The database username
  12.             String password = "password"; // The database password
  13.             Connection = DriverManager.getConnection(url, username, password);
  14.             System.out.println("Now connected to the SQL server");
  15.         } catch (ClassNotFoundException e) {
  16.             System.out.println("Driver class not found.");
  17.         } catch (SQLException e) {
  18.             System.out.println("Error connecting to the SQL server");
  19.         }
  20.     }
  21.  
  22.     public static void destroyConnection() {
  23.         try {
  24.             if(Connection != null) Connection.close();
  25.         } catch (Exception e) {
  26.         }
  27.     }
  28.     public static void executeQuery(String mquery) {
  29.         try {
  30.             Statement stmt = Connection.createStatement();
  31.             stmt.executeUpdate(mquery);
  32.             stmt.close();
  33.         } catch (Exception e) { e.printStackTrace();}
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement