Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. package org.soaa.services;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.sql.ResultSet;
  7.  
  8. public class TestJDBC {
  9. public static void main(String[] args){
  10.  
  11.  
  12. Statement stmt = null;
  13. ResultSet rs = null;
  14. Connection conn = null;
  15.  
  16. try {
  17.  
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver").newInstance();
  20. } catch (InstantiationException e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. } catch (IllegalAccessException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. } catch (ClassNotFoundException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. }
  30. conn =
  31. DriverManager.getConnection("jdbc:mysql://localhost/dbbib?" +
  32. "user=root&password=kevin");
  33. stmt = conn.createStatement();
  34. rs = stmt.executeQuery("SELECT * FROM articles");
  35. // or alternatively, if you don't know ahead of time that
  36. // the query will be a SELECT...
  37. if (stmt.execute("SELECT * FROM articles")) {
  38. rs = stmt.getResultSet();
  39. }
  40. rs.next();
  41. System.out.println(rs.getString(1));
  42. System.out.println(rs.getString(2));
  43. System.out.println(rs.getString(3));
  44. // Now do something with the ResultSet ....
  45. }
  46. catch (SQLException ex){
  47. // handle any errors
  48. System.out.println("SQLException: " + ex.getMessage());
  49. System.out.println("SQLState: " + ex.getSQLState());
  50. System.out.println("VendorError: " + ex.getErrorCode());
  51. }
  52. finally {
  53. // it is a good idea to release
  54. // resources in a finally{} block
  55. // in reverse-order of their creation
  56. // if they are no-longer needed
  57. if (rs != null) {
  58. try {
  59. rs.close();
  60. } catch (SQLException sqlEx) { } // ignore
  61. rs = null;
  62. }
  63. if (stmt != null) {
  64. try {
  65. stmt.close();
  66. } catch (SQLException sqlEx) { } // ignore
  67. stmt = null;
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement