Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- NetworkServerControl server=new NetworkServerControl(InetAddress.getLocalHost(),1527);
- server.start (null);
- //Class.forName n DriverManager.getConnection() declarations goes here.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement