Guest User

Untitled

a guest
Nov 8th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package connect;
  2. import java.sql.*;
  3. public class MY_Database {
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7.  
  8. String url = "jdbc:mysql://localhost:3306/ ";
  9. String user = "root";
  10. String password = "";
  11. try
  12. {
  13. Class.forName("com.mysql.jdbc.Driver").newInstance();
  14. Connection con= DriverManager.getConnection(url,user,password);
  15. Statement stt = con.createStatement();
  16.  
  17. //Create and Save db
  18.  
  19. stt.execute("Create Database");
  20. stt.execute("Use the test ");
  21.  
  22. //Create out table
  23. stt.execute("DROP TABLE IF EXIST people");
  24. stt.execute("CREATE TABLE people("
  25. +"id BIGINT NOT NULL AUTO_INCREMENT,"
  26. +"fname VARCHAR(25), "
  27. +"lname VARCHAR (25),"
  28. +"PRIMARY KEY (id)"
  29. +
  30. " )");
  31. // ADD SOME ENTRIES
  32. stt.execute("INSERT INTO people(fname , lname ) VALUES "+
  33. "('Nathan','Tenderere') ('Praise ''Tenderere') ('Tadiwa
  34. ''Machona')");
  35.  
  36. // Get people with surname Tenderere
  37. ResultSet res = stt.executeQuery("SELECT * FROM people WHERE lname =
  38. 'Tenderere' ");
  39.  
  40. while (res.next()) {
  41. System.out.println(res.getString("fname")+""+
  42. res.getString("lname") );
  43. }
  44. }
  45. catch(Exception e)
  46. {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. }
Add Comment
Please, Sign In to add comment