我建議你使用一個名爲ConnectionDerby類,其中把所有的邏輯和參數選擇,插入,更新,刪除,並作爲一個嵌入式數據庫我comprobate如果數據庫已經存在,如果不存在我創建然後,我希望這段代碼可以幫助你,抱歉或我的英語,我是新手在java中使用這個數據庫,但我希望這可以幫助你理解...
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;
}
}
}
下面是一個很好的開始:http://db.apache.org/derby/docs/10.6/getstart/特別是,請按照本教程中的步驟操作;您可以找到相當多的示例代碼來幫助您查看步驟:http://db.apache.org/derby/docs/10.6/getstart/cgstutorialintro.html – 2010-12-09 05:42:25
這是最好的。簡潔和完整: http://www.codejava.net/java-se/jdbc/connect-to-apache-derby-java-db-via-jdbc – Bipul 2014-01-19 15:26:25