Guest User

Untitled

a guest
Jan 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package sqltest;
  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.  
  10. public class SQLtest {
  11.  
  12.  
  13. private static final String url = "jdbc:mysql://localhost:3306/school_db?useTimezone=true&serverTimezone=GMT";
  14. private static final String user = "root";
  15. private static final String password = "123456";
  16.  
  17.  
  18.  
  19.  
  20.  
  21. private static Connection con;
  22. private static Statement stmt;
  23. private static ResultSet rs;
  24.  
  25. public static void main(String args[]) {
  26. String query = "select count(*) from student";
  27.  
  28. try {
  29.  
  30. con = DriverManager.getConnection(url, user, password);
  31.  
  32.  
  33. stmt = con.createStatement();
  34.  
  35.  
  36. rs = stmt.executeQuery(query);
  37.  
  38. while (rs.next()) {
  39. int count = rs.getInt(1);
  40. System.out.println("Students : " + count);
  41. }
  42.  
  43. } catch (SQLException sqlEx) {
  44. sqlEx.printStackTrace();
  45. } finally {
  46.  
  47. try { con.close(); } catch(SQLException se) { }
  48. try { stmt.close(); } catch(SQLException se) { }
  49. try { rs.close(); } catch(SQLException se) { }
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. public static void main(String args[]) {
  57. String query = "select * from student";
  58.  
  59. try {
  60. con = DriverManager.getConnection(url, user, password);
  61. stmt = con.createStatement();
  62. rs = stmt.executeQuery(query);
  63. while (rs.next()) {
  64. int id = rs.getInt(1);
  65. String firstName = rs.getString(2);
  66. String lastName = rs.getString(3);
  67. System.out.println("Students : " + count + " " + firstname + " " + lastName);
  68. }
  69.  
  70. } catch (SQLException sqlEx) {
  71. sqlEx.printStackTrace();
  72. } finally {
  73.  
  74. try { con.close(); } catch(SQLException se) { }
  75. try { stmt.close(); } catch(SQLException se) { }
  76. try { rs.close(); } catch(SQLException se) { }
  77. }
Add Comment
Please, Sign In to add comment