Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication;
- import java.io.*;
- import java.sql.*;
- public class Database
- {
- Connection conn;
- public Database(String username, String password, String fileName) throws Exception
- {
- //Load HSQLDB driver
- Class.forName("org.hsqldb.jdbcDriver");
- //Connect to database
- conn = DriverManager.getConnection("jdbc:hsqldb:file:"
- + username,
- password,
- fileName);
- }
- public int update(String sqlStatement) throws SQLException
- {
- //Execute an SQL update
- Statement st = null;
- st = conn.createStatement(); // statements
- int i = st.executeUpdate(sqlStatement); // run the query
- if (i == -1)
- {
- System.out.println("db error : " + sqlStatement);
- }
- st.close();
- return i;
- }
- public ResultSet query(String sqlStatement) throws SQLException
- {
- //Execute an SQL query
- Statement st= null;
- ResultSet rs = null;
- st = conn.createStatement();
- rs = st.executeQuery(sqlStatement);
- st.close();
- return rs;
- }
- public void close()throws SQLException
- {
- //Close database connection
- Statement st = conn.createStatement();
- st.execute("SHUTDOWN");
- conn.close();
- }
- }
Add Comment
Please, Sign In to add comment