Clase conexiónDB
Código PHP:
public class ConexionDB {
String user;
String pass;
String url;
static Connection conex;
static Statement stm;
static ResultSet rs;
public ConexionDB() {
user = "alumno";
pass = "alumno";
url = "jdbc:oracle:thin:@PCVIRTUAL:1521:BASE";
conex = null;
stm = null;
}
public void conectar() {
try {
Class.forName("oracle.jdbc.OracleDriver").newInstance();
conex = DriverManager.getConnection(url, user, pass);
JOptionPane.showMessageDialog(null, "Conexion realizada con éxito");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Conexion fallida");
JOptionPane.showInputDialog(e, null);
}
}
public ResultSet LeerDatosDB() throws SQLException {
try {
stm = conex.createStatement();
String cadSql = "";
cadSql = "SELECT * FROM PRODUCTOS ORDER BY id";
rs = stm.executeQuery(cadSql);
} catch (SQLException ex) {
Logger.getLogger(ConexionDB.class.getName()).log(Level.SEVERE,
null, ex);
} finally {
// stm.close();
}
return rs;
}
public ResultSet buscarCMP(String mp) throws SQLException {
try {
stm = conex.createStatement();
String cadSQL = "";
System.out.println(mp);
cadSQL = "SELECT * FROM MATERIAPRIMA WHERE CODMP = '" + mp + "'";
rs = stm.executeQuery(cadSQL);
if (!rs.next()) {
JOptionPane.showMessageDialog(null, "No existe tal registro");
VerCMP.codigoMP.setText("");
} else {
//return rs;
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "No existe tal registro");
} finally {
//stm.close();
}
return rs;
}
Clase MenuGUI
Código PHP:
LISTAR = new JButton("VER PIEZAS EN STOCK");
LISTAR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
VerCMP.main(null);
/*String codMP = JOptionPane.showInputDialog("Introduzca el código de Materia Prima");
try {
objConex.buscarCMP(codMP);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
});
LISTAR.setBounds(10, 11, 164, 42);
getContentPane().add(LISTAR);
Clase: VerCMP
Código PHP:
public class VerCMP extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static JTextField codigoMP;
private JButton btnBuscar;
ConexionDB objConex = new ConexionDB();
ResultSet rs;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VerCMP frame = new VerCMP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public VerCMP() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 342, 144);
getContentPane().setLayout(null);
codigoMP = new JTextField();
codigoMP.setText("Introduce Código de Materia Prima");
codigoMP.setBounds(10, 40, 213, 20);
getContentPane().add(codigoMP);
codigoMP.setColumns(10);
btnBuscar = new JButton("BUSCAR");
btnBuscar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String codMP= codigoMP.getText();
try {
objConex.buscarCMP(codMP);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnBuscar.setBounds(233, 39, 91, 23);
getContentPane().add(btnBuscar);