Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import com.mysql.jdbc.Statement;
  8.  
  9. public class DatabaseConnection {
  10. // JDBC driver name and database URL
  11. private final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  12. private String DB_URL = "jdbc:mysql://localhost/";
  13. private String DB_NAME;
  14. private Connection connection = null;
  15.  
  16. // Database credentials
  17. static final String USER = "user";
  18. static final String PASS = "pass";
  19.  
  20. public DatabaseConnection(String databaseName) {
  21. DB_NAME = databaseName;
  22. DB_URL = DB_URL + DB_NAME;
  23. }
  24.  
  25. public Connection getConnection() {
  26. if (connection == null) {
  27. connection = getNewConnection();
  28. }
  29.  
  30. return connection;
  31. }
  32.  
  33.  
  34. public Connection getNewConnection() {
  35. try{
  36. //STEP 2: Register JDBC driver
  37. Class.forName("com.mysql.jdbc.Driver");
  38.  
  39. //STEP 3: Open a connection
  40. System.out.println("Connecting to database...");
  41. connection = DriverManager.getConnection(DB_URL,USER,PASS);
  42. System.out.println("Connected successfully");
  43. return connection;
  44.  
  45. }catch(SQLException se){
  46. //Handle errors for JDBC
  47. se.printStackTrace();
  48. }catch(Exception e){
  49. //Handle errors for Class.forName
  50. e.printStackTrace();
  51. }
  52. return null;
  53. }
  54.  
  55.  
  56. public static void main(String[] args) {
  57.  
  58. DatabaseConnection db = new DatabaseConnection("students");
  59. Connection conn = db.getConnection();
  60.  
  61. Statement stmt = null;
  62. try{
  63. //STEP 4: Execute a query
  64. System.out.println("Creating statement...");
  65. stmt = (Statement) conn.createStatement();
  66. String sql;
  67. sql = "SELECT id, name FROM students";
  68. ResultSet rs = stmt.executeQuery(sql);
  69.  
  70. //STEP 5: Extract data from result set
  71. while(rs.next()){
  72. //Retrieve by column name
  73. int id = rs.getInt("id");
  74. // int age = rs.getInt("age");
  75. String name = rs.getString("name");
  76. // Float mark = rs.getFloat("mark");
  77.  
  78. //Display values
  79. System.out.print("ID: " + id);
  80. // System.out.print(", Age: " + age);
  81. System.out.print(", Name: " + name);
  82. // System.out.println(", Mark: " + mark);
  83. }
  84. //STEP 6: Clean-up environment
  85. rs.close();
  86. stmt.close();
  87. conn.close();
  88. }catch(SQLException se){
  89. //Handle errors for JDBC
  90. se.printStackTrace();
  91. }catch(Exception e){
  92. //Handle errors for Class.forName
  93. e.printStackTrace();
  94. }finally{
  95. //finally block used to close resources
  96. try{
  97. if(stmt!=null)
  98. stmt.close();
  99. }catch(SQLException se2){
  100. }// nothing we can do
  101. try{
  102. if(conn!=null)
  103. conn.close();
  104. }catch(SQLException se){
  105. se.printStackTrace();
  106. }//end finally try
  107. }//end try
  108. System.out.println("Goodbye!");
  109. }//end main
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement