在独立Java应用程序中设置嵌入式Derby数据库 [英] Setting up an embedded Derby database in a standalone Java application
问题描述
我正在尝试为独立的Java应用程序设置嵌入式Derby数据库,但在完成各种文档之后,我似乎无法找到任何简单的解释或示例。我正在使用带有Derby插件的Eclipse,并为我的项目启用了Derby特性。
I'm trying to setup an embedded Derby database for a standalone Java application, but after pouring through all sorts of documentation, I just can't seem to find any simple explanations or examples. I'm using Eclipse with the Derby plugin and have enabled Derby nature for my project.
我找到了在独立地址簿以及在Eclipse中使用Derby的概述(似乎不太可能)覆盖嵌入式部署,但我仍然觉得我缺少一些基本的东西。
I found an example of using an embedded Derby database in a standalone address book as well as an overview of using Derby in Eclipse (that doesn't seem to cover the embedded deployment), but I still feel like I'm missing something fundamental.
这是我第一次尝试使用带有Java的数据库,而且我有点困惑,所以这是我的基本问题:
This is my first time attempting to use a database with Java, and I'm a little confused, so here are my basic questions:
- Java与Derby数据库交互的基本原理(或模型)是什么? (在嵌入式部署中)?是否要遵循他们重要的设计模式?
- 我是否需要在类中创建某种类型的数据库构造函数(包括表结构等),或者是否需要在某些类中创建其他工具?
- 创建并保存了一个数据库,如何启动它?实际数据库保存在哪里?
代码片段非常有用!
推荐答案
我建议您使用名为ConnectionDerby的类,其中将所有逻辑和参数放入选择,插入,更新,删除,并作为嵌入式数据库,如果是数据库已经存在,如果不存在我创建了,我希望这段代码可以帮助你,抱歉或我的英语和我是新手在java中使用这个数据库,但我希望这有助于你理解....
I suggest that you use a class named ConnectionDerby, where put all the logic and the parameters to Select, insert, update, Delete, and as a embedded database i comprobate if a database already exists, if not exists i created then, i hope this code help you, sorry or my english and i am newbie using this database in java, but i hope this help you to understand....
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class ConnectionDerby {
private Connection conn = null;
private Statement sttm = null;
public Connection CrearBD(String query) {
try {
//Obtenemos el Driver de Derby
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection("jdbc:derby:.\\BD\\nombrebasededatos.db;create=true");
if (conn != null) {
//JOptionPane.showMessageDialog(null, "Base de Datos Lista");
try {
PreparedStatement pstm = conn.prepareStatement(query);
pstm.execute();
pstm.close();
//JOptionPane.showMessageDialog(null, "Base de Datos Creada Correctamente");
System.out.println("SENTENCIA SQL EFECTUADA CORRECTAMENTE");
} catch (SQLException ex) {
//JOptionPane.showMessageDialog(null, ex.getLocalizedMessage());
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN EJECUTAR LAS SENTENCIAS SQL parte 2");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN EJECUTAR LAS SENTENCIAS SQL parte 3");
}
return conn;
}
public Connection AccederBD() {
try {
//Obtenemos el Driver de Derby
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Obtenemos la Conexión
conn = DriverManager.getConnection("jdbc:derby:.\\BD\\nombrebasededatos.db");
if (conn != null) {
System.out.println("Base de Datos Ya Leida Correctamente");
//JOptionPane.showMessageDialog(null, "Base de Datos Ya Leida Correctamente");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("Sistema Creado por Mario José Echeverría");
System.out.println("NO SE ENCONTRO LA BASE DE DATOS");
System.out.println("CREANDO BASE DE DATOS EN DERBY DATABASE");
String createTableProyecto = "Sentence to create first table";
String createTablePrimer = "Sentence to create second table";
String createTableTopCoat = "Sentence to create third table";
String createTableCotizacion = "Sentence to create fourth table";
CrearBD(createTableProyecto);
CrearBD(createTablePrimer);
CrearBD(createTableTopCoat);
CrearBD(createTableCotizacion);
//*************PRUEBAS*****************
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
System.out.println("ERROR DE TIPO ClassNotFoundException");
//JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN ACCEDER A LA BASE DE DATOS parte 2");
}
return conn;
}
public void UID(String sqlcad) {
try {
//Obtenemos el Driver de Derby
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection("jdbc:derby:.\\BD\\nombrebasededatos.db");
sttm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
sttm.executeUpdate(sqlcad);
System.out.println("Conexión Exitosa a la Base de Datos");
//JOptionPane.showMessageDialog(null, "Conexión exitosa");
sttm.close();
conn.close();
if (conn != null) {
System.out.println("Consulta Realizada Correctamente");
//JOptionPane.showMessageDialog(null, "Base de Datos Ya Leida Correctamente");
}
} catch (SQLException e) {
System.out.println("Error= " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Error= " + e.getMessage());
}
}
public ResultSet getvalores(String sqlcad) {
ResultSet rs = null;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection("jdbc:derby:.\\BD\\nombrebasededatos.db");
sttm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//String sqlcad = "Select nombre, m2xgal, pregal, precub, descripcion from primer";
rs = sttm.executeQuery(sqlcad);
return rs;
} catch (Exception e) {
System.out.println("Error= " + e.getMessage());
return rs;
}
}
}
这篇关于在独立Java应用程序中设置嵌入式Derby数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!