Created
July 2, 2015 11:01
-
-
Save d3ep4k/2f6e4d7bb8786e5b9fec to your computer and use it in GitHub Desktop.
DB Helper to perform CRUD operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.sql.Connection; | |
| import java.sql.DriverManager; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Properties; | |
| public class DBHelper { | |
| Properties props = new Properties(); | |
| Connection connection = null; | |
| PreparedStatement statement; | |
| ResultSet resultSet; | |
| public DBHelper() throws IOException, ClassNotFoundException, SQLException { | |
| props.load(DBHelper.class.getResourceAsStream("sql.properties")); | |
| Class.forName("org.sqlite.JDBC"); | |
| connection = DriverManager.getConnection("jdbc:sqlite:test.db"); | |
| } | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| try { | |
| DBHelper db = new DBHelper(); | |
| List list = new ArrayList(); | |
| list.add(43134); | |
| list.add("dhanraj thakare"); | |
| list.add(23); | |
| list.add(229.43); | |
| // db.updateQuery("insert", list); | |
| list = new ArrayList(); | |
| list.add(44); | |
| db.updateQuery("delete_id", list); | |
| db.displayRecords(); | |
| db.close(); | |
| } catch (SQLException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } catch (ClassNotFoundException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| void displayRecords() throws SQLException { | |
| resultSet = selectQuery("select_all", new ArrayList<Object>()); | |
| System.out.println("Name" + "\t" + "Salary" + "\t" + "ID" + "\t" | |
| + "Age"); | |
| while (resultSet.next()) { | |
| String coffeeName = resultSet.getString("NAME"); | |
| double price = resultSet.getDouble("SALARY"); | |
| int sales = resultSet.getInt("ID"); | |
| int total = resultSet.getInt("AGE"); | |
| System.out.println(coffeeName + "\t" + price + "\t" + sales + "\t" | |
| + total); | |
| } | |
| resultSet.close(); | |
| statement.close(); | |
| } | |
| /** | |
| * Select query to fetch results based on values | |
| * | |
| * @param sql | |
| * The prepared statement sql query | |
| * @param values | |
| * Values for prepared statement | |
| * @return Resultset of the query | |
| * @throws SQLException | |
| */ | |
| ResultSet selectQuery(String sql, List<Object> values) throws SQLException { | |
| prepareStatement(sql, values); | |
| resultSet = statement.executeQuery(); | |
| return resultSet; | |
| } | |
| void updateQuery(String sql, List<Object> values) throws SQLException { | |
| prepareStatement(sql, values); | |
| statement.executeUpdate(); | |
| statement.close(); | |
| } | |
| void prepareStatement(String sql, List values) throws SQLException { | |
| statement = connection.prepareStatement(props.getProperty(sql)); | |
| int i = 1; | |
| for (Object obj : values) { | |
| if (obj instanceof Integer) { | |
| statement.setInt(i++, (Integer) obj); | |
| } else if (obj instanceof String) { | |
| statement.setString(i++, (String) obj); | |
| } else if (obj instanceof Double) { | |
| statement.setDouble(i++, (Double) obj); | |
| } else if (obj instanceof Float) { | |
| statement.setFloat(i++, (Float) obj); | |
| } | |
| } | |
| } | |
| void close() throws SQLException { | |
| resultSet.close(); | |
| statement.close(); | |
| connection.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment