Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class JDBCTest {
  4. public static void main(String[] args) throws SQLException {
  5. Connection myConn = null;
  6. Statement myStmt = null;
  7. ResultSet myRs = null;
  8.  
  9. try {
  10. myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LibSys", "root", "");
  11. System.out.println("Database connection successful!\n");
  12.  
  13. myStmt = myConn.createStatement();
  14. myRs = myStmt.executeQuery("select * from books");
  15.  
  16. while (myRs.next()) {
  17. System.out.println(myRs.getString("book_name"));
  18. }
  19. }
  20. catch (Exception exc) {
  21. exc.printStackTrace();
  22. }
  23. finally {
  24. if (myRs != null) {
  25. myRs.close();
  26. }
  27.  
  28. if (myStmt != null) {
  29. myStmt.close();
  30. }
  31.  
  32. if (myConn != null) {
  33. myConn.close();
  34. }
  35.  
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement