Advertisement
gagan93

MySql Demo

Aug 16th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3.  
  4. public class MySQLDemo
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         try
  9.         {
  10.             Connection conn = null;
  11.  
  12.             /* Assuming that your database is on localhost */
  13.             String url = "jdbc:mysql://localhost:3306/";
  14.  
  15.             /* Assumign name of your database is 'newdb' */
  16.             String dbName = "newdb";
  17.  
  18.             /* default driver name */
  19.             String driver = "com.mysql.jdbc.Driver";
  20.  
  21.             /* Default username is 'root' */
  22.             String userName = "root";
  23.  
  24.             /* Default password is blank */
  25.             String password = "";
  26.  
  27.             Class.forName(driver).newInstance();
  28.             conn = DriverManager
  29.                     .getConnection(url + dbName, userName, password);
  30.            
  31.             /*  If something like this
  32.              * (Connection is com.mysql.jdbc.JDBC4Connection@2fecdb1b)
  33.              *  is printed without exception, then connection is established*/
  34.             System.out.println("Connection is " + conn);
  35.         }
  36.         catch (Exception e)
  37.         {
  38.             System.out.println(e);
  39.             e.printStackTrace();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement