In this last part of the series, I will show how to save and retrieve data from our embedded database.
- Create a new class called DBConect under the package org.embedded.db. Replace with the following code:
- package org.embedded.db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import org.openide.util.Exceptions;
- public class DBConect {
- Connection con = null;
- public DBConect() {
- Properties prop = System.getProperties();
- prop.put("derby.system.home",
System.getProperty("user.home") + "/" + ".mesh"); - try {
- //start the database and get a connection
- con = DriverManager.
getConnection("jdbc:derby:mesh_db", prop); - } catch (SQLException e) {
- Exceptions.printStackTrace(e);
- }
- }
- public ResultSet executeQuery(String sql) throws SQLException {
- Statement statement = con.createStatement();
- return statement.executeQuery(sql);
- }
- public int executeUpdate(String sql) throws SQLException {
- Statement statement = con.createStatement();
- return statement.executeUpdate(sql);
- }
- }
- Now you can use this class to query the database or save data to the database using the two functions provided. This is a simple class. You can extend/modify it to do more complicated tasks.
No comments:
Post a Comment