java应用于嵌入derby_在独立Java应用程序中设置嵌入式Derby数据库

我建议您使用一个名为ConnectionDerby的类,其中将所有逻辑和参数选择,插入,更新,删除,并作为嵌入式数据库,如果数据库已存在,则进行复制,如果不存在,则我希望此代码帮助你,对不起或我的英语,我是新手在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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值