Guest User

Untitled

a guest
Jan 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class ClassMid2 {
  4.  
  5. private final String DB_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
  6. private final String USER_ID = "scott";
  7. private final String PASSWORD = "tiger";
  8. private Connection con;
  9.  
  10. public ClassMid2() {
  11. try {
  12. con = DriverManager.getConnection(DB_URL, USER_ID, PASSWORD);
  13. } catch (SQLException ex) {
  14. System.err.println(ex.getMessage());
  15. }
  16. }
  17.  
  18. public void disconnectFromDB() {
  19. try {
  20. con.close();
  21. } catch (SQLException e) {
  22. System.err.println(e.getMessage());
  23. }
  24. }
  25.  
  26. public void queryMid2() {
  27. String stmntStr = "SELECT * FROM Mid2";
  28. ResultSet rs;
  29.  
  30. try {
  31. Statement stmnt = con.createStatement();
  32. rs = stmnt.executeQuery(stmntStr);
  33.  
  34. while (rs.next()) {
  35. System.out.print(rs.getString(1));
  36. System.out.print("\t");
  37. System.out.print(rs.getString(2));
  38. System.out.print("\t");
  39. System.out.print(rs.getInt(3));
  40. System.out.println();
  41. }
  42.  
  43. } catch (SQLException ex) {
  44. System.err.println(ex.getMessage());
  45. }
  46. }
  47.  
  48. public static void main(String[] args) {
  49. ClassMid2 cm2 = new ClassMid2();
  50. cm2.queryMid2();
  51. cm2.disconnectFromDB();
  52. }
  53.  
  54. }
Add Comment
Please, Sign In to add comment