Your SlideShare is downloading. ×
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

EJEMPLOS DESARROLLADOS

12,600

Published on

Published in: Education
8 Comments
30 Likes
Statistics
Notes
No Downloads
Views
Total Views
12,600
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
8
Likes
30
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. EJEMPLOS DESARROLLADOS (NETBEANS – SQL SERVER)<br />MANTENIMIENTO DE CLIENTE<br />----------------------------------------------------------------<br />package alfa;<br />import java.sql.*;<br />import javax.swing.*;<br />public class mancli extends javax.swing.JFrame {<br /> private Connection cn;<br /> private Statement st;<br /> private ResultSet rs;<br /> private boolean rnuevo = false;<br /> private String cod = "";<br /> private String cmdSQL = "";<br /> public mancli() {<br /> initComponents();<br /> }<br /> private void formWindowOpened(java.awt.event.WindowEvent evt) { <br /> try {<br /> conexion();<br /> rs.next();<br /> mostrar();<br /> cn.setAutoCommit(false);// los cambios deben conf<br /> } catch (SQLException ex) {<br /> System.err.println("Error en conexion:" + ex.getMessage());<br /> }<br /> } <br /> private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.first();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.last();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isFirst()) {<br /> mensaje("Primer registro");<br /> } else {<br /> rs.previous();<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isLast()) {<br /> mensaje("Ultimo registro");<br /> } else {<br /> rs.next();<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { <br /> txtCodcli.setText("");<br /> txtCliente.setText("");<br /> txtDireccion.setText("");<br /> txtLcredito.setText("");<br /> chkExtranjero.setSelected(false);<br /> setControles(false);<br /> setEditar(true);<br /> txtCodcli.requestFocus();<br /> rnuevo = true;<br /> } <br /> private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) { <br /> setControles(false);<br /> setEditar(true);<br /> cod = txtCodcli.getText().trim();<br /> txtCodcli.requestFocus();<br /> } <br /> private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) { <br /> boolean conf = confirmax("Cancelar cambios");<br /> if (conf) {<br /> setControles(true);<br /> setEditar(false);<br /> rnuevo = false;<br /> mostrar();<br /> }<br /> } <br /> private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) { <br /> boolean conf = confirmax("Guardar cambios");<br /> if (conf) {<br /> try {<br /> int x;<br /> x = chkExtranjero.isSelected() ? 1 : 0;<br /> if (rnuevo) {<br /> cmdSQL = "Insert into Clientes (Cod_cli,Cliente,Direccion,Lcredito,Extranjero)"<br /> + "Values('"<br /> + txtCodcli.getText().trim()<br /> + "','"<br /> + txtCliente.getText().trim()<br /> + "','"<br /> + txtDireccion.getText().trim()<br /> + "',"<br /> + txtLcredito.getText().trim()<br /> + ","<br /> + String.valueOf(x) + ")";<br /> st.executeUpdate(cmdSQL);<br /> mensaje("Registro adicionado");<br /> rnuevo = false;<br /> } else {<br /> cmdSQL = "Update Clientes set cod_cli='"<br /> + txtCodcli.getText().trim()<br /> + "',cliente='"<br /> + txtCliente.getText().trim()<br /> + "',Direccion='"<br /> + txtDireccion.getText().trim()<br /> + "',Lcredito="<br /> + txtLcredito.getText().trim()<br /> + ",Extranjero=" + String.valueOf(x)<br /> + " where cod_cli='" + cod + "'";<br /> st.executeUpdate(cmdSQL);<br /> mensaje("Registro modificado");<br /> }<br /> cmdSQL = "Select * from Clientes";<br /> rs = st.executeQuery(cmdSQL);<br /> setControles(true);<br /> setEditar(false);<br /> rs.next();<br /> mostrar();<br /> } catch (Exception x) {<br /> System.err.println("Error al guardar:" + x.getMessage());<br /> }<br /> }<br /> } <br /> private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) { <br /> boolean conf = confirmax("Eliminar registro");<br /> if (conf) {<br /> try {<br /> cmdSQL = "Delete from Clientes where "<br /> + "cod_cli='" + txtCodcli.getText().trim()<br /> + "'";<br /> st.executeUpdate(cmdSQL);<br /> // rs.first();<br /> cmdSQL = "Select * from Clientes";<br /> rs = st.executeQuery(cmdSQL);<br /> rs.next();<br /> mostrar();<br /> } catch (Exception x) {<br /> }<br /> }<br /> } <br /> private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) { <br /> String codbus = JOptionPane.showInputDialog(null,<br /> "Ingrese codigo a Buscar");<br /> boolean encontrado = busqueda(codbus);<br /> if (encontrado) {<br /> mostrar();<br /> } else {<br /> mensaje("Codigo no encontrado");<br /> }<br /> } <br /> private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) { <br /> boolean salirsi = confirmax("Cerrar formulario");<br /> if (salirsi) {<br /> try {<br /> boolean conf = confirmax("Guardar todos los cambios");<br /> if (conf) {<br /> cn.commit();// confirma cambios<br /> } else {<br /> cn.rollback(); // deshace cambios<br /> }<br /> cn.setAutoCommit(true);// los cambios no se conf<br /> rs.close();<br /> st.close();<br /> cn.close();<br /> mensaje("Conexion cerrada");<br /> System.exit(0);<br /> } catch (Exception x) {<br /> System.out.println("Error al cerrar tabla Clientes:"<br /> + x.getMessage());<br /> }<br /> //ocultar();<br /> }<br /> } <br /> // Metodos del usuario<br /> public void mostrar() {<br /> try {<br /> rs.refreshRow();<br /> txtCodcli.setText(rs.getString("cod_cli"));<br /> rs.refreshRow();<br /> txtCliente.setText(rs.getString("cliente"));<br /> rs.refreshRow();<br /> txtDireccion.setText(rs.getString("direccion"));<br /> rs.refreshRow();<br /> txtLcredito.setText(String.valueOf(rs.getFloat("lcredito")));<br /> rs.refreshRow();<br /> chkExtranjero.setSelected(rs.getBoolean("extranjero"));<br /> } catch (Exception ex) {<br /> System.err.println("Error en registro:" + ex.getMessage());<br /> }<br /> }<br /> public static boolean confirmax(String msj) {<br /> int r = JOptionPane.showConfirmDialog(null, msj, "",<br /> JOptionPane.YES_NO_OPTION);<br /> if (r == JOptionPane.YES_OPTION) {<br /> return true;<br /> } else {<br /> return false;<br /> }<br /> }<br /> public void setControles(boolean estado) {<br /> btnPrimero.setEnabled(estado);<br /> btnAnterior.setEnabled(estado);<br /> btnSiguiente.setEnabled(estado);<br /> btnUltimo.setEnabled(estado);<br /> btnNuevo.setEnabled(estado);<br /> btnModificar.setEnabled(estado);<br /> btnEliminar.setEnabled(estado);<br /> btnBuscar.setEnabled(estado);<br /> btnPrimero.setEnabled(estado);<br /> btnAnterior.setEnabled(estado);<br /> btnSiguiente.setEnabled(estado);<br /> btnSalir.setEnabled(estado);<br /> btnGuardar.setEnabled(!estado);<br /> btnCancelar.setEnabled(!estado);<br /> }<br /> public void setEditar(boolean estado) {<br /> txtCodcli.setEditable(estado);<br /> txtCliente.setEditable(estado);<br /> txtDireccion.setEditable(estado);<br /> txtLcredito.setEditable(estado);<br /> chkExtranjero.setEnabled(estado);<br /> }<br /> public boolean busqueda(String xcod) {<br /> boolean encontrado = false;<br /> int nra;<br /> try {<br /> nra = rs.getRow();<br /> rs.beforeFirst();<br /> boolean neof = rs.next();<br /> while (neof && !encontrado) {<br /> if (rs.getString("cod_cli").equals(xcod.trim())) {<br /> encontrado = true;<br /> } else {<br /> neof = rs.next();<br /> }<br /> }<br /> if (!encontrado) {<br /> rs.absolute(nra);<br /> }<br /> } catch (Exception x) {<br /> System.err.println("Error en busqueda:" + x.getMessage());<br /> }<br /> return encontrado;<br /> }<br /> public void conexion() {<br /> try {<br /> String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";<br /> String url = "jdbc:sqlserver://localhost;databaseName=Facturacion";<br /> /*String driver = "sun.jdbc.odbc.JdbcOdbcDriver";<br /> String url = "jdbc:odbc:SQLFacturacion";*/<br /> Class.forName(driver);<br /> cn = DriverManager.getConnection(url, "sa", "123");<br /> st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,<br /> ResultSet.CONCUR_UPDATABLE);<br /> rs = st.executeQuery("Select * from Clientes");<br /> } catch (Exception ex) {<br /> System.err.println("Error en busqueda:" + ex.getMessage());<br /> }<br /> }<br /> public void mensaje(String msj) {<br /> JOptionPane.showMessageDialog(null, msj);<br /> }<br /> public static void main(String args[]) {<br /> java.awt.EventQueue.invokeLater(new Runnable() {<br /> public void run() {<br /> new mancli().setVisible(true);<br /> }<br /> });<br /> }<br />BUSCAR CLIENTES<br />-----------------------------------------------------------<br />package alfa;<br />import java.sql.Connection;<br />import java.sql.DriverManager;<br />import java.sql.ResultSet;<br />import java.sql.Statement;<br />import javax.swing.DefaultListModel;<br />public class Buscli extends javax.swing.JFrame {<br /> Connection cn = null; // variable de conexion<br /> Statement st = null; // variable de instruccion SQL<br /> ResultSet rs = null; // variable de registros o filas<br /> public Buscli() {<br /> initComponents();<br /> }<br />private void formWindowOpened(java.awt.event.WindowEvent evt) { <br /> conexion();<br /> llenarLista();<br /> } <br /> private void txtClibusKeyReleased(java.awt.event.KeyEvent evt) { <br /> String cmdSQL = "";<br /> cmdSQL = "Select * from Clientes Where cliente Like '"<br /> + txtClibus.getText().trim() + "%'";<br /> try {<br /> rs = st.executeQuery(cmdSQL);<br /> } catch (Exception ex) {<br /> }<br /> llenarLista();<br /> } <br />private void conexion() {<br /> try {<br /> // odbc<br /> //String driver = "sun.jdbc.odbc.JdbcOdbcDriver";<br /> //String url = "jdbc:odbc:SQLFacturacion";<br /> // sql Server nativo<br /> String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";<br /> String url = "jdbc:sqlserver://localhost;databaseName=Facturacion";<br /> Class.forName(driver);<br /> cn = DriverManager.getConnection(url, "sa", "123");<br /> st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,<br /> ResultSet.CONCUR_READ_ONLY);<br /> rs = st.executeQuery("Select * from Clientes");<br /> } catch (Exception ex) {<br /> System.err.println("Error en conexion:" + ex.getMessage());<br /> }<br /> }<br /> private void llenarLista() {<br /> modelo.clear(); // vaciamos la lista<br /> try {<br /> rs.beforeFirst();<br /> while (rs.next()) {<br /> modelo.addElement(rs.getString("cod_cli") + " " + rs.getString("cliente"));<br /> }<br /> } catch (Exception ex) {<br /> }<br /> }<br /> public static void main(String args[]) {<br /> java.awt.EventQueue.invokeLater(new Runnable() {<br /> public void run() {<br /> new Buscli().setVisible(true);<br /> }<br /> });<br /> }<br />VISUALIZAR CLIENTES (DRIVER SQL)<br />-------------------------------------------------------------<br />package alfa;<br />import java.sql.Connection;<br />import java.sql.DriverManager;<br />import java.sql.ResultSet;<br />import java.sql.Statement;<br />import javax.swing.JOptionPane;<br />public class Viscli extends javax.swing.JFrame {<br /> // variables globales<br /> Connection cn = null; // variable de conexion<br /> Statement st = null; // variable de instruccion SQL<br /> ResultSet rs = null; // variable de registros o filas<br /> /** Creates new form Viscli */<br /> public Viscli() {<br /> initComponents();<br /> }<br />private void formWindowOpened(java.awt.event.WindowEvent evt) { <br /> conexion();<br /> } <br /> private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.first();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.last();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isFirst()) {<br /> JOptionPane.showMessageDialog(null, "Inicio de la tabla");<br /> } else {<br /> rs.previous(); // ir al anterior registro<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isLast()) {<br /> JOptionPane.showMessageDialog(null, "Final de la tabla");<br /> } else {<br /> rs.next();<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) { <br /> int rsp;<br /> rsp = JOptionPane.showConfirmDialog(null, "Cerrar formulario",<br /> "Salir", JOptionPane.YES_NO_OPTION);<br /> if (rsp == JOptionPane.YES_OPTION) {<br /> this.dispose();<br /> }<br /> } <br /> private void conexion() {<br /> try {<br /> // odbc<br /> //String driver = "sun.jdbc.odbc.JdbcOdbcDriver";<br /> //String url = "jdbc:odbc:SQLFacturacion";<br /> // sql Server nativo<br /> String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";<br /> String url = "jdbc:sqlserver://localhost;databaseName=Facturacion";<br /> Class.forName(driver);<br /> cn = DriverManager.getConnection(url, "sa", "123");<br /> st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,<br /> ResultSet.CONCUR_READ_ONLY);<br /> rs = st.executeQuery("Select * from Clientes");<br /> rs.next();<br /> mostrar();<br /> } catch (Exception ex) {<br /> System.err.println("Error en conexion:" + ex.getMessage());<br /> }<br /> }<br /> private void mostrar() {<br /> try {<br /> txtCodcli.setText(rs.getString("cod_cli"));<br /> txtCliente.setText(rs.getString("cliente"));<br /> txtDireccion.setText(rs.getString("direccion"));<br /> txtLcredito.setText(String.valueOf(rs.getFloat("Lcredito")));<br /> chkExtranjero.setSelected(rs.getBoolean("Extranjero"));<br /> } catch (Exception ex) {<br /> System.err.println("Error en registro:" + ex.getMessage());<br /> }<br /> }<br /> public static void main(String args[]) {<br /> java.awt.EventQueue.invokeLater(new Runnable() {<br /> public void run() {<br /> new Viscli().setVisible(true);<br /> }<br /> });<br /> }<br />VISUALIZAR CLIENTES (DRIVER NATIVO)<br />--------------------------------------------------------------<br />package alfa;<br />import java.sql.Connection;<br />import java.sql.DriverManager;<br />import java.sql.ResultSet;<br />import java.sql.Statement;<br />import javax.swing.JOptionPane;<br />public class Viscli extends javax.swing.JFrame {<br /> Connection cn = null; // variable de conexion<br /> Statement st = null; // variable de instruccion SQL<br /> ResultSet rs = null; // variable de registros o filas<br /> public Viscli() {<br /> initComponents();<br /> }<br /> private void formWindowOpened(java.awt.event.WindowEvent evt) { <br /> conexion();<br /> } <br /> private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.first();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> rs.last();<br /> mostrar();<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isFirst()) {<br /> JOptionPane.showMessageDialog(null, "Inicio de la tabla");<br /> } else {<br /> rs.previous(); // ir al anterior registro<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) { <br /> try {<br /> if (rs.isLast()) {<br /> JOptionPane.showMessageDialog(null, "Final de la tabla");<br /> } else {<br /> rs.next();<br /> mostrar();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> } <br /> private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) { <br /> int rsp;<br /> rsp = JOptionPane.showConfirmDialog(null, "Cerrar formulario",<br /> "Salir", JOptionPane.YES_NO_OPTION);<br /> if (rsp == JOptionPane.YES_OPTION) {<br /> this.dispose();<br /> }<br /> } <br /> private void conexion() {<br /> try {<br /> String driver = "sun.jdbc.odbc.JdbcOdbcDriver";<br /> String url = "jdbc:odbc:SQLFacturacion";<br /> Class.forName(driver);<br /> cn = DriverManager.getConnection(url, "sa", "123");<br /> st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,<br /> ResultSet.CONCUR_READ_ONLY);<br /> rs = st.executeQuery("Select * from Clientes");<br /> rs.next();<br /> mostrar();<br /> } catch (Exception ex) {<br /> System.err.println("Error en conexion:" + ex.getMessage());<br /> }<br /> }<br /> private void mostrar() {<br /> try {<br /> txtCodcli.setText(rs.getString("cod_cli"));<br /> txtCliente.setText(rs.getString("cliente"));<br /> txtDireccion.setText(rs.getString("direccion"));<br /> txtLcredito.setText(String.valueOf(rs.getFloat("Lcredito")));<br /> chkExtranjero.setSelected(rs.getBoolean("Extranjero"));<br /> } catch (Exception ex) {<br /> System.err.println("Error en registro:" + ex.getMessage());<br /> }<br /> }<br /> public static void main(String args[]) {<br /> java.awt.EventQueue.invokeLater(new Runnable() {<br /> public void run() {<br /> new Viscli().setVisible(true);<br /> }<br /> });<br /> }<br />

×