Advertisement
Guest User

test

a guest
Feb 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package com.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class dbexample {
  10.  
  11.     public dbexample () throws Exception {
  12.        
  13.         Connection connection=null;
  14.         Statement statement = null;
  15.        
  16.         try
  17.         {
  18.             // Register the JDBS driver
  19.             Class.forName("com.mysql.jdbc.Driver");
  20.             System.out.println("Database is registered");
  21.            
  22.             //Opening Database connection
  23.             connection =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dicetest", "root", "mirchi89");
  24.            
  25.             String sql = "select * from users";
  26.             statement = connection.createStatement();
  27.             ResultSet rs = statement.executeQuery(sql);
  28.            
  29.             while(rs.next()){
  30.                 String studentname = rs.getString("email");
  31.                 System.out.println(studentname);
  32.             }
  33.            
  34.            
  35.         } catch (ClassNotFoundException e) {
  36.        
  37.             e.printStackTrace();
  38.         }
  39.         finally {
  40.             connection.close();
  41.         }
  42.        
  43.  
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement