Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.Statement;
  3.  
  4. public class DatabaseConnection {
  5.     Statement myStatHandle;
  6.  
  7.     public DatabaseConnection() {
  8.  
  9.     }
  10.  
  11.     public boolean connect() {
  12.         try {
  13.  
  14.             // get an instance of the driver
  15.  
  16.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  17.  
  18.             // enter the details for your database user account
  19.  
  20.             String myUsername = "root";
  21.  
  22.             String myPassword = "pass";
  23.  
  24.             // the URL and DBMS for your database
  25.  
  26.             String myDatabaseURL = "jdbc:mysql://DATABASESERVER:3307/test";
  27.  
  28.             // get a connection to the database
  29.  
  30.             java.sql.Connection myDBConnection = DriverManager
  31.                     .getConnection(myDatabaseURL,myUsername,myPassword);
  32.  
  33.             // create a statement handle instance to carry out queries
  34.  
  35.             myStatHandle = myDBConnection.createStatement();
  36.  
  37.             // return true for successful connection
  38.             return true;
  39.         }
  40.  
  41.         catch (Exception E)
  42.  
  43.         {
  44.             System.out.println(E.getMessage());
  45.             return false;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement