Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication;
  6.  
  7. import java.io.*;
  8. import java.sql.*;
  9.  
  10.  
  11. public class Database
  12. {
  13. Connection conn;
  14. public Database(String username, String password, String fileName) throws Exception
  15.  
  16. {
  17. //Load HSQLDB driver
  18.  
  19. Class.forName("org.hsqldb.jdbcDriver");
  20.  
  21. //Connect to database
  22.  
  23. conn = DriverManager.getConnection("jdbc:hsqldb:file:"
  24. + username,
  25. password,
  26. fileName);
  27. }
  28.  
  29. public int update(String sqlStatement) throws SQLException
  30. {
  31. //Execute an SQL update
  32. Statement st = null;
  33.  
  34. st = conn.createStatement(); // statements
  35.  
  36. int i = st.executeUpdate(sqlStatement); // run the query
  37.  
  38. if (i == -1)
  39. {
  40. System.out.println("db error : " + sqlStatement);
  41. }
  42.  
  43. st.close();
  44. return i;
  45.  
  46. }
  47. public ResultSet query(String sqlStatement) throws SQLException
  48. {
  49. //Execute an SQL query
  50. Statement st= null;
  51. ResultSet rs = null;
  52.  
  53. st = conn.createStatement();
  54.  
  55. rs = st.executeQuery(sqlStatement);
  56.  
  57. st.close();
  58.  
  59. return rs;
  60. }
  61.  
  62.  
  63. public void close()throws SQLException
  64. {
  65. //Close database connection
  66.  
  67. Statement st = conn.createStatement();
  68.  
  69. st.execute("SHUTDOWN");
  70. conn.close();
  71. }
  72. }
Add Comment
Please, Sign In to add comment