Advertisement
Guest User

asdasdasd

a guest
Sep 29th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. package EjercicioDeInicio;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import java.awt.BorderLayout;
  7. import javax.swing.JList;
  8. import javax.swing.JPanel;
  9. import javax.swing.DefaultListModel;
  10. import javax.swing.JButton;
  11. import javax.swing.JTextField;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.MouseAdapter;
  15. import java.awt.event.MouseEvent;
  16. import java.io.BufferedReader;
  17. import java.io.FileReader;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.PrintWriter;
  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.PreparedStatement;
  24. import java.sql.SQLException;
  25.  
  26. import javax.swing.ListSelectionModel;
  27.  
  28. public class ejercicio {
  29.  
  30. private JFrame frame;
  31. private JTextField texto;
  32. private JList<String> list;
  33.  
  34. /**
  35. * Launch the application.
  36. */
  37. public static void main(String[] args) {
  38. EventQueue.invokeLater(new Runnable() {
  39. public void run() {
  40. try {
  41. ejercicio window = new ejercicio();
  42. window.frame.setVisible(true);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. });
  48. }
  49.  
  50. /**
  51. * Create the application.
  52. */
  53. public ejercicio() {
  54. initialize();
  55. }
  56.  
  57. /**
  58. * Initialize the contents of the frame.
  59. */
  60. private void initialize() {
  61. frame = new JFrame();
  62. frame.setBounds(100, 100, 714, 377);
  63. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. frame.getContentPane().setLayout(new BorderLayout(0, 0));
  65.  
  66. list = new JList<String>();
  67. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  68. list.addMouseListener(new MouseAdapter() {
  69. @Override
  70. public void mouseClicked(MouseEvent e) {
  71. listSelected();
  72. }
  73. });
  74. DefaultListModel<String> model = new DefaultListModel<String>();
  75. list.setModel(model);
  76.  
  77. frame.getContentPane().add(list, BorderLayout.CENTER);
  78.  
  79. JPanel panel = new JPanel();
  80. frame.getContentPane().add(panel, BorderLayout.SOUTH);
  81.  
  82. JButton delete = new JButton("Borrar seleccionado");
  83. delete.addMouseListener(new MouseAdapter() {
  84. @Override
  85. public void mouseClicked(MouseEvent arg0) {
  86. deleteElement();
  87. }
  88. });
  89. panel.add(delete);
  90.  
  91. JButton savedisc = new JButton("Guardar en disco");
  92. savedisc.addMouseListener(new MouseAdapter() {
  93. @Override
  94. public void mouseClicked(MouseEvent e) {
  95. saveDisk();
  96. }
  97. });
  98. savedisc.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent arg0) {
  100. }
  101. });
  102. panel.add(savedisc);
  103.  
  104. JButton loaddisc = new JButton("Cargar de disco");
  105. loaddisc.addMouseListener(new MouseAdapter() {
  106. @Override
  107. public void mouseClicked(MouseEvent e) {
  108. loadDisk();
  109. }
  110. });
  111. panel.add(loaddisc);
  112.  
  113. JButton savedb = new JButton("Guardar en base de datos");
  114. savedb.addMouseListener(new MouseAdapter() {
  115. @Override
  116. public void mouseClicked(MouseEvent arg0) {
  117. saveDB();
  118. }
  119. });
  120. panel.add(savedb);
  121.  
  122. JButton loaddb = new JButton("Cargar en base de datos");
  123. panel.add(loaddb);
  124.  
  125. JPanel panel_1 = new JPanel();
  126. frame.getContentPane().add(panel_1, BorderLayout.WEST);
  127.  
  128. texto = new JTextField();
  129. panel_1.add(texto);
  130. texto.setColumns(10);
  131.  
  132. JButton add = new JButton("A\u00F1adir");
  133. add.addMouseListener(new MouseAdapter() {
  134. @Override
  135. public void mouseClicked(MouseEvent arg0) {
  136. addElement();
  137. }
  138. });
  139. panel_1.add(add);
  140.  
  141. JButton edit = new JButton("Modificar");
  142. edit.addMouseListener(new MouseAdapter() {
  143. @Override
  144. public void mouseClicked(MouseEvent e) {
  145. modifyElement();
  146. }
  147. });
  148. panel_1.add(edit);
  149. }
  150.  
  151. private void deleteElement() {
  152. String selectedValue = list.getSelectedValue();
  153. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  154. model.removeElement(selectedValue);
  155. }
  156.  
  157. private void addElement() {
  158. String value = texto.getText();
  159. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  160. model.addElement(value);
  161. }
  162.  
  163. private void listSelected() {
  164. String selectedValue = list.getSelectedValue();
  165. texto.setText(selectedValue);
  166. }
  167.  
  168. private void modifyElement() {
  169. String value = texto.getText();
  170. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  171. if (list.getSelectedIndex() > -1)
  172. model.setElementAt(value, list.getSelectedIndex());
  173. }
  174.  
  175. private void saveDisk() {
  176. FileWriter fw = null;
  177. PrintWriter pw = null;
  178. try {
  179. fw = new FileWriter("fichero.txt");
  180. pw = new PrintWriter(fw);
  181.  
  182. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  183. int size = model.getSize();
  184. for (int i = 0; i < size; i++) {
  185. pw.println(model.getElementAt(i));
  186. }
  187.  
  188. /*for (int i = 0; i < 0; i++) {
  189. pw.println("hola");
  190. }*/
  191.  
  192. } catch (IOException e) {
  193. System.out.println("Fallo en el acceso al fichero." + e.getMessage());
  194. } finally {
  195. try {
  196. if (fw != null)
  197. fw.close();
  198. if (pw != null)
  199. pw.close();
  200. } catch (IOException e) {
  201. // TODO Auto-generated catch block
  202. e.printStackTrace();
  203. }
  204. }
  205. }
  206.  
  207. private void loadDisk() {
  208. FileReader fr = null;
  209. BufferedReader br = null;
  210. try {
  211. fr = new FileReader("fichero.txt");
  212. br = new BufferedReader(fr);
  213.  
  214. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  215. model.removeAllElements();
  216. String line;
  217. while ((line = br.readLine()) != null) {
  218. model.addElement(line);
  219. }
  220. } catch (IOException e) {
  221. System.out.println("Fallo en el acceso al fichero." + e.getMessage());
  222. } finally {
  223. try {
  224. if (fr != null)
  225. fr.close();
  226. if (br != null)
  227. br.close();
  228. } catch (IOException e) {
  229. // TODO Auto-generated catch block
  230. e.printStackTrace();
  231. }
  232. }
  233. }
  234.  
  235. private void saveDB() {
  236. String sURL = "jdbc:mysql://localhost:3306/lista";
  237. String user = "root";
  238. String pass = "AlumnoDAM";
  239. String sDriver = "com.mysql.jdbc.Driver";
  240. Connection con = null;
  241.  
  242. try {
  243. Class.forName(sDriver).newInstance();
  244. con = DriverManager.getConnection(sURL, user, pass);
  245. } catch (SQLException e) {
  246. // TODO Auto-generated catch block
  247. e.printStackTrace();
  248. } catch (InstantiationException e) {
  249. // TODO Auto-generated catch block
  250. e.printStackTrace();
  251. } catch (IllegalAccessException e) {
  252. // TODO Auto-generated catch block
  253. e.printStackTrace();
  254. } catch (ClassNotFoundException e) {
  255. // TODO Auto-generated catch block
  256. e.printStackTrace();
  257. }
  258.  
  259. DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
  260. int size = model.getSize();
  261. for (int i = 0; i < size; i++) {
  262. String query = "INSERT INTO lista (nombre) VALUES (?)";
  263. PreparedStatement ps = con.prepareStatement(query);
  264. ps.setString(1, "xxxx");
  265. ps.executeUpdate();
  266. }
  267.  
  268. con.close();
  269. }
  270.  
  271. public JList<String> getList() {
  272. return list;
  273. }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement