Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- public class Catalogo {
- static int highId;
- final static ArrayList buffer = new ArrayList();
- static JFrame frame;
- private static JTextField searchField;
- private static JTextField txtFName;
- private static JTextField txtFId;
- private static JTextField catNameAdd;
- private static JTextField catIdAdd;
- static int newId;
- static String nextId;
- /**
- * Launch the application.
- * @wbp.parser.entryPoint
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Catalogo window = new Catalogo();
- Catalogo.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- }
- }
- });
- }
- /**
- * Create the application.
- * @wbp.parser.entryPoint
- */
- public Catalogo() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private static void initialize() {
- Connection con = null;
- java.sql.Statement st = null;
- ResultSet rs = null;
- String url = "jdbc:mysql://localhost:3306/";
- String db = "FAVEGA";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "";
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url + db, user, pass);
- con.setAutoCommit(false);
- st = con.createStatement();
- String sql = "SELECT id FROM catalogos";
- rs = st.executeQuery(sql);
- while (rs.next()) {
- String highId = (rs.getString("id"));
- newId = Integer.parseInt(highId);
- newId = newId + 1;
- nextId = Integer.toString(newId);
- }
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- frame = new JFrame();
- frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Catalogo.class.getResource("/javax/swing/plaf/metal/icons/ocean/directory.gif")));
- frame.setTitle("Gesti\u00F3n de cat\u00E1logos");
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- final JTabbedPane tab = new JTabbedPane();
- frame.getContentPane().add(tab, BorderLayout.CENTER);
- final JPanel buscarPanel = new JPanel();
- tab.addTab("Buscar", null, buscarPanel, null);
- buscarPanel.setLayout(null);
- JLabel lblBuscar = new JLabel("Buscar:");
- lblBuscar.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblBuscar.setBounds(10, 14, 47, 22);
- buscarPanel.add(lblBuscar);
- JButton btnBuscar = new JButton("Buscar");
- btnBuscar.setBounds(346, 16, 79, 23);
- buscarPanel.add(btnBuscar);
- final JButton btnEditar = new JButton("Editar");
- btnEditar.setBounds(107, 207, 79, 23);
- buscarPanel.add(btnEditar);
- btnEditar.setVisible(false);
- final JButton btnReiniciar = new JButton("Reiniciar");
- btnReiniciar.setBounds(209, 207, 93, 23);
- buscarPanel.add(btnReiniciar);
- //PLACE WITH ERROR
- btnBuscar.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- String search = searchField.getText();
- Connection con = null;
- java.sql.Statement st = null;
- ResultSet rs = null;
- String url = "jdbc:mysql://localhost:3306/";
- String db = "FAVEGA";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "";
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url + db, user, pass);
- con.setAutoCommit(false);
- st = con.createStatement();
- String sql = "SELECT * FROM catalogos WHERE id = '" + search + "' OR name LIKE '%" + search + "%' OR keywords LIKE '%" + search + "%'";
- rs = st.executeQuery(sql);
- while (rs.next()) {
- final String resultName = rs.getString("name");
- buffer.add(resultName);
- }
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }finally{
- btnEditar.setVisible(true);
- btnReiniciar.setVisible(true);
- final JList list = new JList(buffer.toArray());
- final JScrollPane scrollPane = new JScrollPane(list);
- scrollPane.add(list);
- scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
- scrollPane.setBounds(67, 195, 269, -143);
- buscarPanel.add(scrollPane);
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- list.setBounds(0, 0, 435, 240);
- scrollPane.setPreferredSize(new Dimension(70, 80));
- list.setLayoutOrientation(JList.VERTICAL);
- buscarPanel.invalidate();
- buscarPanel.validate();
- btnEditar.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- if(list.getSelectedValue() != null){
- String selectedValue = (String) list.getSelectedValue();
- tab.setSelectedIndex(2);
- btnReiniciar.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- scrollPane.setVisible(false);
- btnEditar.setVisible(false);
- btnReiniciar.setVisible(false);
- }
- });
- }
- }
- });
- }
- }
- });
- searchField = new JTextField();
- searchField.setBounds(67, 16, 269, 22);
- buscarPanel.add(searchField);
- searchField.setColumns(10);
- btnReiniciar.setVisible(false);
- JPanel editPanel = new JPanel();
- editPanel.setLayout(null);
- tab.addTab("Editar", null, editPanel, null);
- JLabel lblNombre = new JLabel("Nombre de la empresa:");
- lblNombre.setForeground(Color.BLACK);
- lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblNombre.setBounds(10, 44, 152, 14);
- editPanel.add(lblNombre);
- txtFName = new JTextField(7);
- txtFName.setBounds(172, 42, 251, 22);
- editPanel.add(txtFName);
- JLabel lblKeywords = new JLabel("Palabras clave:");
- lblKeywords.setForeground(Color.BLACK);
- lblKeywords.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblKeywords.setBounds(10, 69, 104, 26);
- editPanel.add(lblKeywords);
- Button btnActualizar = new Button("Actualizar");
- btnActualizar.setActionCommand("");
- btnActualizar.setBounds(353, 190, 70, 22);
- editPanel.add(btnActualizar);
- JLabel lblHelp1 = new JLabel("Las palabras clave se introducen en min\u00FAsculas");
- lblHelp1.setHorizontalAlignment(SwingConstants.CENTER);
- lblHelp1.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblHelp1.setBounds(4, 186, 343, 22);
- editPanel.add(lblHelp1);
- JLabel lblHelp2 = new JLabel(" y separadas por espacios en blanco.");
- lblHelp2.setHorizontalAlignment(SwingConstants.CENTER);
- lblHelp2.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblHelp2.setBounds(10, 202, 337, 26);
- editPanel.add(lblHelp2);
- JScrollPane scrollPane_1 = new JScrollPane();
- scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- scrollPane_1.setBounds(172, 71, 251, 113);
- editPanel.add(scrollPane_1);
- final JTextArea txtFKeywords = new JTextArea(7, 6);
- txtFKeywords.setLineWrap(true);
- scrollPane_1.setViewportView(txtFKeywords);
- JLabel lblCatNum = new JLabel("N\u00FAmero de cat\u00E1logo:");
- lblCatNum.setForeground(Color.BLACK);
- lblCatNum.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblCatNum.setBounds(10, 15, 152, 14);
- editPanel.add(lblCatNum);
- txtFId = new JTextField(4);
- txtFId.setToolTipText("");
- txtFId.setText(nextId);
- txtFId.setEditable(false);
- txtFId.setBounds(172, 11, 251, 22);
- editPanel.add(txtFId);
- JPanel addPanel = new JPanel();
- addPanel.setLayout(null);
- tab.addTab("A\u00F1adir", null, addPanel, null);
- JLabel label = new JLabel("Nombre de la empresa:");
- label.setForeground(Color.BLACK);
- label.setFont(new Font("Tahoma", Font.PLAIN, 14));
- label.setBounds(10, 44, 152, 14);
- addPanel.add(label);
- catNameAdd = new JTextField(7);
- catNameAdd.setBounds(172, 42, 251, 22);
- addPanel.add(catNameAdd);
- JLabel label_1 = new JLabel("Palabras clave:");
- label_1.setForeground(Color.BLACK);
- label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
- label_1.setBounds(10, 69, 104, 26);
- addPanel.add(label_1);
- Button btnAdd = new Button("A\u00F1adir");
- btnAdd.setBounds(360, 190, 63, 22);
- addPanel.add(btnAdd);
- JLabel label_2 = new JLabel("Las palabras clave se introducen en min\u00FAsculas");
- label_2.setHorizontalAlignment(SwingConstants.CENTER);
- label_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
- label_2.setBounds(4, 186, 343, 22);
- addPanel.add(label_2);
- JLabel label_3 = new JLabel(" y separadas por espacios en blanco.");
- label_3.setHorizontalAlignment(SwingConstants.CENTER);
- label_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
- label_3.setBounds(10, 202, 337, 26);
- addPanel.add(label_3);
- JScrollPane scrollPane_2 = new JScrollPane();
- scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- scrollPane_2.setBounds(172, 71, 251, 113);
- addPanel.add(scrollPane_2);
- final JTextArea catKeyAdd = new JTextArea(7, 6);
- catKeyAdd.setLineWrap(true);
- scrollPane_2.setViewportView(catKeyAdd);
- JLabel label_4 = new JLabel("N\u00FAmero de cat\u00E1logo:");
- label_4.setForeground(Color.BLACK);
- label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
- label_4.setBounds(10, 15, 152, 14);
- addPanel.add(label_4);
- catIdAdd = new JTextField(4);
- catIdAdd.setToolTipText("");
- catIdAdd.setText(nextId);
- catIdAdd.setEditable(false);
- catIdAdd.setBounds(172, 11, 251, 22);
- addPanel.add(catIdAdd);
- btnAdd.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- Connection con = null;
- java.sql.Statement st = null;
- String url = "jdbc:mysql://localhost:3306/";
- String db = "favega";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "";
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url + db, user, pass);
- con.setAutoCommit(true);
- st = con.createStatement();
- String sql = "INSERT INTO `catalogos` (`name`, `keywords`) VALUES(" + "'" + catNameAdd.getText() + "', '" + catKeyAdd + "')";
- st.executeUpdate(sql);
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- finally{
- JOptionPane.showMessageDialog(frame,
- "Añadido correctamente catálogo numero " + catIdAdd.getText() + " de la empresa " + catNameAdd.getText() + " y con las palabras clave: " + catKeyAdd + " .");
- }
- }
- });
- btnActualizar.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- Connection con = null;
- java.sql.Statement st = null;
- String url = "jdbc:mysql://localhost:3306/";
- String db = "favega";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "";
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url + db, user, pass);
- con.setAutoCommit(true);
- st = con.createStatement();
- String sql = "UPDATE `catalogos` SET name='" + txtFName.getText() + "', keywords='" + txtFKeywords.getText() + "' WHERE id=" + txtFId.getText();
- st.executeUpdate(sql);
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- finally{
- JOptionPane.showMessageDialog(frame,
- "Editado correctamente catálogo numero " + txtFId.getText() + " de la empresa " + txtFName.getText() + " y con las palabras clave: " + txtFKeywords.getText() + " .");
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement