Guest User

Untitled

a guest
Jan 8th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package mysqlconn;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6.  
  7. public class Mysqlconn {
  8.  
  9. public static void main(String[] args)throws SQLException {
  10.  
  11. try {
  12. String url="jdbc:mysql://localhost:3306/newdatabase";
  13.  
  14. Properties prop=new Properties();
  15. prop.setProperty("user","root");
  16. prop.setProperty("password","");
  17.  
  18. Driver d = new com.mysql.jdbc.Driver();
  19. Connection con = d.connect(url,prop);
  20.  
  21. if(con == null) {
  22. System.out.println("connection failed");
  23. return;
  24. }
  25.  
  26. String query = "SELECT *FROM user";
  27.  
  28. Statement state = con.createStatement();
  29. ResultSet result = null;
  30.  
  31. state.executeQuery(query);
  32.  
  33. while(result.next()){
  34. // fetching result from database
  35. String fname = result.getString("first_name");
  36. String lname = result.getString("last_name");
  37. int age = result.getInt("age");
  38.  
  39. System.out.println("First Name: " + fname + ", Last Name: " + lname + ", Age: " + age);
  40. }
  41.  
  42. state.close();
  43. } catch (Exception e)
  44. {
  45. System.err.println("Got an exception!");
  46. System.err.println(e.getMessage());
  47. }
  48.  
  49. }
  50. }
Add Comment
Please, Sign In to add comment