Advertisement
Guest User

Untitled

a guest
Jul 11th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.17 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3.  
  4. public class Catalogo {
  5. static int highId;
  6. final static ArrayList buffer = new ArrayList();
  7. static JFrame frame;
  8. private static JTextField searchField;
  9. private static JTextField txtFName;
  10. private static JTextField txtFId;
  11. private static JTextField catNameAdd;
  12. private static JTextField catIdAdd;
  13. static int newId;
  14. static String nextId;
  15.  
  16. /**
  17. * Launch the application.
  18. * @wbp.parser.entryPoint
  19. */
  20.  
  21. public static void main(String[] args) {
  22. EventQueue.invokeLater(new Runnable() {
  23. public void run() {
  24. try {
  25. Catalogo window = new Catalogo();
  26. Catalogo.frame.setVisible(true);
  27.  
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }finally{
  31. }
  32.  
  33.  
  34. }
  35. });
  36. }
  37.  
  38. /**
  39. * Create the application.
  40. * @wbp.parser.entryPoint
  41. */
  42.  
  43. public Catalogo() {
  44. initialize();
  45. }
  46.  
  47. /**
  48. * Initialize the contents of the frame.
  49. */
  50. private static void initialize() {
  51. Connection con = null;
  52. java.sql.Statement st = null;
  53. ResultSet rs = null;
  54.  
  55. String url = "jdbc:mysql://localhost:3306/";
  56. String db = "FAVEGA";
  57. String driver = "com.mysql.jdbc.Driver";
  58. String user = "root";
  59. String pass = "";
  60. try {
  61. Class.forName(driver);
  62. con = DriverManager.getConnection(url + db, user, pass);
  63. con.setAutoCommit(false);
  64. st = con.createStatement();
  65. String sql = "SELECT id FROM catalogos";
  66. rs = st.executeQuery(sql);
  67. while (rs.next()) {
  68. String highId = (rs.getString("id"));
  69. newId = Integer.parseInt(highId);
  70. newId = newId + 1;
  71. nextId = Integer.toString(newId);
  72. }
  73. } catch (ClassNotFoundException e1) {
  74. // TODO Auto-generated catch block
  75. e1.printStackTrace();
  76. } catch (SQLException e1) {
  77. // TODO Auto-generated catch block
  78. e1.printStackTrace();
  79. }
  80. frame = new JFrame();
  81. frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Catalogo.class.getResource("/javax/swing/plaf/metal/icons/ocean/directory.gif")));
  82. frame.setTitle("Gesti\u00F3n de cat\u00E1logos");
  83. frame.setBounds(100, 100, 450, 300);
  84. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85.  
  86. final JTabbedPane tab = new JTabbedPane();
  87. frame.getContentPane().add(tab, BorderLayout.CENTER);
  88.  
  89. final JPanel buscarPanel = new JPanel();
  90. tab.addTab("Buscar", null, buscarPanel, null);
  91. buscarPanel.setLayout(null);
  92.  
  93. JLabel lblBuscar = new JLabel("Buscar:");
  94. lblBuscar.setFont(new Font("Tahoma", Font.PLAIN, 14));
  95. lblBuscar.setBounds(10, 14, 47, 22);
  96. buscarPanel.add(lblBuscar);
  97. JButton btnBuscar = new JButton("Buscar");
  98. btnBuscar.setBounds(346, 16, 79, 23);
  99. buscarPanel.add(btnBuscar);
  100.  
  101. final JButton btnEditar = new JButton("Editar");
  102. btnEditar.setBounds(107, 207, 79, 23);
  103. buscarPanel.add(btnEditar);
  104. btnEditar.setVisible(false);
  105.  
  106. final JButton btnReiniciar = new JButton("Reiniciar");
  107. btnReiniciar.setBounds(209, 207, 93, 23);
  108. buscarPanel.add(btnReiniciar);
  109.  
  110.  
  111. //PLACE WITH ERROR
  112. btnBuscar.addActionListener(new ActionListener(){
  113. public void actionPerformed(ActionEvent e) {
  114.  
  115. String search = searchField.getText();
  116. Connection con = null;
  117. java.sql.Statement st = null;
  118. ResultSet rs = null;
  119.  
  120. String url = "jdbc:mysql://localhost:3306/";
  121. String db = "FAVEGA";
  122. String driver = "com.mysql.jdbc.Driver";
  123. String user = "root";
  124. String pass = "";
  125. try {
  126. Class.forName(driver);
  127. con = DriverManager.getConnection(url + db, user, pass);
  128. con.setAutoCommit(false);
  129. st = con.createStatement();
  130. String sql = "SELECT * FROM catalogos WHERE id = '" + search + "' OR name LIKE '%" + search + "%' OR keywords LIKE '%" + search + "%'";
  131. rs = st.executeQuery(sql);
  132. while (rs.next()) {
  133. final String resultName = rs.getString("name");
  134. buffer.add(resultName);
  135. }
  136. } catch (ClassNotFoundException e1) {
  137. // TODO Auto-generated catch block
  138. e1.printStackTrace();
  139. } catch (SQLException e1) {
  140. // TODO Auto-generated catch block
  141. e1.printStackTrace();
  142. }finally{
  143. btnEditar.setVisible(true);
  144. btnReiniciar.setVisible(true);
  145. final JList list = new JList(buffer.toArray());
  146. final JScrollPane scrollPane = new JScrollPane(list);
  147. scrollPane.add(list);
  148. scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
  149. scrollPane.setBounds(67, 195, 269, -143);
  150. buscarPanel.add(scrollPane);
  151. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  152. list.setBounds(0, 0, 435, 240);
  153.  
  154. scrollPane.setPreferredSize(new Dimension(70, 80));
  155. list.setLayoutOrientation(JList.VERTICAL);
  156. buscarPanel.invalidate();
  157. buscarPanel.validate();
  158. btnEditar.addActionListener(new ActionListener(){
  159. public void actionPerformed(ActionEvent e) {
  160. if(list.getSelectedValue() != null){
  161. String selectedValue = (String) list.getSelectedValue();
  162. tab.setSelectedIndex(2);
  163. btnReiniciar.addActionListener(new ActionListener(){
  164. public void actionPerformed(ActionEvent e) {
  165. scrollPane.setVisible(false);
  166. btnEditar.setVisible(false);
  167. btnReiniciar.setVisible(false);
  168. }
  169. });
  170. }
  171. }
  172. });
  173. }
  174. }
  175. });
  176.  
  177.  
  178. searchField = new JTextField();
  179. searchField.setBounds(67, 16, 269, 22);
  180. buscarPanel.add(searchField);
  181. searchField.setColumns(10);
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. btnReiniciar.setVisible(false);
  191.  
  192. JPanel editPanel = new JPanel();
  193. editPanel.setLayout(null);
  194. tab.addTab("Editar", null, editPanel, null);
  195.  
  196. JLabel lblNombre = new JLabel("Nombre de la empresa:");
  197. lblNombre.setForeground(Color.BLACK);
  198. lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 14));
  199. lblNombre.setBounds(10, 44, 152, 14);
  200. editPanel.add(lblNombre);
  201.  
  202. txtFName = new JTextField(7);
  203. txtFName.setBounds(172, 42, 251, 22);
  204. editPanel.add(txtFName);
  205.  
  206. JLabel lblKeywords = new JLabel("Palabras clave:");
  207. lblKeywords.setForeground(Color.BLACK);
  208. lblKeywords.setFont(new Font("Tahoma", Font.PLAIN, 14));
  209. lblKeywords.setBounds(10, 69, 104, 26);
  210. editPanel.add(lblKeywords);
  211.  
  212. Button btnActualizar = new Button("Actualizar");
  213. btnActualizar.setActionCommand("");
  214. btnActualizar.setBounds(353, 190, 70, 22);
  215. editPanel.add(btnActualizar);
  216.  
  217. JLabel lblHelp1 = new JLabel("Las palabras clave se introducen en min\u00FAsculas");
  218. lblHelp1.setHorizontalAlignment(SwingConstants.CENTER);
  219. lblHelp1.setFont(new Font("Tahoma", Font.PLAIN, 12));
  220. lblHelp1.setBounds(4, 186, 343, 22);
  221. editPanel.add(lblHelp1);
  222.  
  223. JLabel lblHelp2 = new JLabel(" y separadas por espacios en blanco.");
  224. lblHelp2.setHorizontalAlignment(SwingConstants.CENTER);
  225. lblHelp2.setFont(new Font("Tahoma", Font.PLAIN, 12));
  226. lblHelp2.setBounds(10, 202, 337, 26);
  227. editPanel.add(lblHelp2);
  228.  
  229. JScrollPane scrollPane_1 = new JScrollPane();
  230. scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  231. scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  232. scrollPane_1.setBounds(172, 71, 251, 113);
  233. editPanel.add(scrollPane_1);
  234.  
  235. final JTextArea txtFKeywords = new JTextArea(7, 6);
  236. txtFKeywords.setLineWrap(true);
  237. scrollPane_1.setViewportView(txtFKeywords);
  238.  
  239. JLabel lblCatNum = new JLabel("N\u00FAmero de cat\u00E1logo:");
  240. lblCatNum.setForeground(Color.BLACK);
  241. lblCatNum.setFont(new Font("Tahoma", Font.PLAIN, 14));
  242. lblCatNum.setBounds(10, 15, 152, 14);
  243. editPanel.add(lblCatNum);
  244.  
  245. txtFId = new JTextField(4);
  246. txtFId.setToolTipText("");
  247. txtFId.setText(nextId);
  248. txtFId.setEditable(false);
  249. txtFId.setBounds(172, 11, 251, 22);
  250. editPanel.add(txtFId);
  251.  
  252. JPanel addPanel = new JPanel();
  253. addPanel.setLayout(null);
  254. tab.addTab("A\u00F1adir", null, addPanel, null);
  255.  
  256. JLabel label = new JLabel("Nombre de la empresa:");
  257. label.setForeground(Color.BLACK);
  258. label.setFont(new Font("Tahoma", Font.PLAIN, 14));
  259. label.setBounds(10, 44, 152, 14);
  260. addPanel.add(label);
  261.  
  262. catNameAdd = new JTextField(7);
  263. catNameAdd.setBounds(172, 42, 251, 22);
  264. addPanel.add(catNameAdd);
  265.  
  266. JLabel label_1 = new JLabel("Palabras clave:");
  267. label_1.setForeground(Color.BLACK);
  268. label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  269. label_1.setBounds(10, 69, 104, 26);
  270. addPanel.add(label_1);
  271.  
  272. Button btnAdd = new Button("A\u00F1adir");
  273. btnAdd.setBounds(360, 190, 63, 22);
  274. addPanel.add(btnAdd);
  275.  
  276. JLabel label_2 = new JLabel("Las palabras clave se introducen en min\u00FAsculas");
  277. label_2.setHorizontalAlignment(SwingConstants.CENTER);
  278. label_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
  279. label_2.setBounds(4, 186, 343, 22);
  280. addPanel.add(label_2);
  281.  
  282. JLabel label_3 = new JLabel(" y separadas por espacios en blanco.");
  283. label_3.setHorizontalAlignment(SwingConstants.CENTER);
  284. label_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
  285. label_3.setBounds(10, 202, 337, 26);
  286. addPanel.add(label_3);
  287.  
  288. JScrollPane scrollPane_2 = new JScrollPane();
  289. scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  290. scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  291. scrollPane_2.setBounds(172, 71, 251, 113);
  292. addPanel.add(scrollPane_2);
  293.  
  294. final JTextArea catKeyAdd = new JTextArea(7, 6);
  295. catKeyAdd.setLineWrap(true);
  296. scrollPane_2.setViewportView(catKeyAdd);
  297.  
  298. JLabel label_4 = new JLabel("N\u00FAmero de cat\u00E1logo:");
  299. label_4.setForeground(Color.BLACK);
  300. label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
  301. label_4.setBounds(10, 15, 152, 14);
  302. addPanel.add(label_4);
  303.  
  304. catIdAdd = new JTextField(4);
  305. catIdAdd.setToolTipText("");
  306. catIdAdd.setText(nextId);
  307. catIdAdd.setEditable(false);
  308. catIdAdd.setBounds(172, 11, 251, 22);
  309. addPanel.add(catIdAdd);
  310.  
  311.  
  312.  
  313. btnAdd.addActionListener(new ActionListener(){
  314. public void actionPerformed(ActionEvent e) {
  315.  
  316. Connection con = null;
  317. java.sql.Statement st = null;
  318.  
  319. String url = "jdbc:mysql://localhost:3306/";
  320. String db = "favega";
  321. String driver = "com.mysql.jdbc.Driver";
  322. String user = "root";
  323. String pass = "";
  324.  
  325.  
  326. try {
  327. Class.forName(driver);
  328. con = DriverManager.getConnection(url + db, user, pass);
  329. con.setAutoCommit(true);
  330. st = con.createStatement();
  331.  
  332. String sql = "INSERT INTO `catalogos` (`name`, `keywords`) VALUES(" + "'" + catNameAdd.getText() + "', '" + catKeyAdd + "')";
  333. st.executeUpdate(sql);
  334. } catch (ClassNotFoundException e1) {
  335. // TODO Auto-generated catch block
  336. e1.printStackTrace();
  337. } catch (SQLException e1) {
  338. // TODO Auto-generated catch block
  339. e1.printStackTrace();
  340. }
  341. finally{
  342. JOptionPane.showMessageDialog(frame,
  343. "Añadido correctamente catálogo numero " + catIdAdd.getText() + " de la empresa " + catNameAdd.getText() + " y con las palabras clave: " + catKeyAdd + " .");
  344. }
  345. }
  346. });
  347.  
  348. btnActualizar.addActionListener(new ActionListener(){
  349. public void actionPerformed(ActionEvent e) {
  350. Connection con = null;
  351. java.sql.Statement st = null;
  352.  
  353. String url = "jdbc:mysql://localhost:3306/";
  354. String db = "favega";
  355. String driver = "com.mysql.jdbc.Driver";
  356. String user = "root";
  357. String pass = "";
  358.  
  359. try {
  360. Class.forName(driver);
  361. con = DriverManager.getConnection(url + db, user, pass);
  362. con.setAutoCommit(true);
  363. st = con.createStatement();
  364.  
  365. String sql = "UPDATE `catalogos` SET name='" + txtFName.getText() + "', keywords='" + txtFKeywords.getText() + "' WHERE id=" + txtFId.getText();
  366. st.executeUpdate(sql);
  367. } catch (ClassNotFoundException e1) {
  368. // TODO Auto-generated catch block
  369. e1.printStackTrace();
  370. } catch (SQLException e1) {
  371. // TODO Auto-generated catch block
  372. e1.printStackTrace();
  373. }
  374. finally{
  375. JOptionPane.showMessageDialog(frame,
  376. "Editado correctamente catálogo numero " + txtFId.getText() + " de la empresa " + txtFName.getText() + " y con las palabras clave: " + txtFKeywords.getText() + " .");
  377. }
  378.  
  379. }
  380. });
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388. }
  389.  
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement