Guest User

Untitled

a guest
Jan 16th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. Students : null null Falcone
  2. Students : null null Wayne
  3. Students : null null Parker
  4. Students : null null Grey
  5. Students : null null Norris
  6. Students : null null Kent
  7.  
  8. package sqltest;
  9.  
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15.  
  16.  
  17. public class SQLtest {
  18.  
  19.  
  20. private static final String url = "jdbc:mysql://localhost:3306/school_db?useTimezone=true&serverTimezone=GMT";
  21. private static final String user = "root";
  22. private static final String password = "123456";
  23.  
  24.  
  25.  
  26.  
  27.  
  28. private static Connection con;
  29. private static Statement stmt;
  30. private static ResultSet rs;
  31.  
  32. public static void main(String args[]) {
  33. String query = "SELECT student.id, student.name ,student.surname from student";
  34.  
  35. try {
  36. con = DriverManager.getConnection(url, user, password);
  37. stmt = con.createStatement();
  38. rs = stmt.executeQuery(query);
  39. while (rs.next()) {
  40. int id = rs.getInt(1);
  41. String firstName = rs.getString(2);
  42. String lastName = rs.getString(3);
  43. String count = null;
  44. String firstname = null;
  45.  
  46.  
  47. System.out.println("Students : " + count + " " + firstname + " " + lastName);
  48. }
  49.  
  50. } catch (SQLException sqlEx) {
  51. sqlEx.printStackTrace();
  52. } finally {
  53.  
  54. try { con.close(); } catch(SQLException se) { }
  55. try { stmt.close(); } catch(SQLException se) { }
  56. try { rs.close(); } catch(SQLException se) { }
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment