Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.87 KB | None | 0 0
  1. //baze de date
  2.  
  3.  
  4. import java.awt.BorderLayout;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridBagLayout;
  7. import java.awt.GridLayout;
  8. import java.awt.HeadlessException;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.Vector;
  18.  
  19. import javax.swing.DefaultListModel;
  20. import javax.swing.JButton;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JList;
  24. import javax.swing.JPanel;
  25. import javax.swing.JTextField;
  26.  
  27.  
  28. class Restaurant
  29. {
  30. String restaurant,specific,zona;
  31.  
  32. public Restaurant(String restaurant, String specific, String zona) {
  33. super();
  34. this.restaurant = restaurant;
  35. this.specific = specific;
  36. this.zona = zona;
  37. }
  38.  
  39. public String getRestaurant() {
  40. return restaurant;
  41. }
  42.  
  43. public void setRestaurant(String restaurant) {
  44. this.restaurant = restaurant;
  45. }
  46.  
  47. public String getSpecific() {
  48. return specific;
  49. }
  50.  
  51. public void setSpecific(String specific) {
  52. this.specific = specific;
  53. }
  54.  
  55. public String getZona() {
  56. return zona;
  57. }
  58.  
  59. public void setZona(String zona) {
  60. this.zona = zona;
  61. }
  62.  
  63. @Override
  64. public String toString() {
  65. return "Restaurant=" + restaurant + ", specific=" + specific + ", zona=" + zona;
  66. }
  67.  
  68.  
  69. }
  70.  
  71. class Window1 extends JFrame
  72. {
  73. static Vector restaurante=new Vector();
  74. static JList lstRestaurante;
  75. static DefaultListModel listM;
  76. public Window1(String title) throws HeadlessException, SQLException {
  77. super(title);
  78. setDefaultCloseOperation(EXIT_ON_CLOSE);
  79. getContentPane().setLayout(new BorderLayout());
  80. setSize(400,400);
  81. listM=new DefaultListModel<>();
  82. lstRestaurante= new JList(listM);
  83.  
  84. String url = "jdbc:mysql://localhost:3306/test";
  85. Connection con=DriverManager.getConnection(url, "root", "root");
  86. Statement stmt = (Statement)con.createStatement();
  87. ResultSet rs=stmt.executeQuery("Select *from restaurante");
  88. JLabel titlu=new JLabel("Lista Restaurante");
  89. getContentPane().add(titlu, BorderLayout.NORTH);
  90. getContentPane().add(lstRestaurante,BorderLayout.CENTER);
  91.  
  92. rs.first();
  93. while(rs.next())
  94. {
  95. Restaurant res=new Restaurant(rs.getString(1),rs.getString(2),rs.getString(3));
  96. restaurante.add(res);
  97. }
  98.  
  99. for(int i=0;i<restaurante.size();i++)
  100. {
  101. Restaurant r = (Restaurant) restaurante.elementAt(i);
  102. listM.addElement(r.toString());
  103. }
  104. }
  105.  
  106. }
  107.  
  108. class Window2 extends JFrame
  109. {
  110. static Vector melodii=new Vector();
  111. static JButton btnSterge= new JButton("Sterge");
  112. static JLabel melodie=new JLabel("Melodie");
  113. static JTextField txtMelodie= new JTextField(15);
  114. static DefaultListModel listMod=new DefaultListModel<>();
  115. static JList lstMelodii=new JList(listMod);
  116. static Connection con;
  117. static Statement stmt;
  118. static ResultSet rs;
  119. public Window2(String title) throws HeadlessException, SQLException {
  120. super(title);
  121. setDefaultCloseOperation(EXIT_ON_CLOSE);
  122. getContentPane().setLayout(new GridLayout(2,1));
  123. setSize(400,400);
  124. JPanel panel1 = new JPanel(new FlowLayout());
  125. panel1.add(melodie);
  126. panel1.add(txtMelodie);
  127. panel1.add(btnSterge);
  128.  
  129. String url = "jdbc:mysql://localhost:3306/test";
  130. con=DriverManager.getConnection(url, "root", "root");
  131. stmt = (Statement)con.createStatement();
  132. rs=stmt.executeQuery("Select *from melodii");
  133.  
  134. getContentPane().add(panel1);
  135. getContentPane().add(lstMelodii);
  136. rs.beforeFirst();
  137. while(rs.next()) {
  138. melodii.add(rs.getString(1));
  139.  
  140. }
  141. for(int i=0;i<melodii.size();i++)
  142. {
  143. listMod.addElement(melodii.elementAt(i));
  144. }
  145. btnSterge.addActionListener(new Clicked());
  146. }
  147. }
  148.  
  149. class Clicked implements ActionListener
  150. {
  151.  
  152. @Override
  153. public void actionPerformed(ActionEvent e) {
  154. String melodieS=Window2.txtMelodie.getText();
  155. for(int i=0;i<Window2.melodii.size();i++)
  156. {
  157. if(Window2.melodii.elementAt(i).equals(melodieS))
  158. {
  159. Window2.melodii.remove(i);
  160. Window2.listMod.removeElementAt(i);
  161. try {
  162. PreparedStatement deleteStmt=Window2.con.prepareStatement("Delete from melodii where melodie=?");
  163. deleteStmt.setString(1, melodieS);
  164. deleteStmt.execute();
  165. Window2.rs=Window2.stmt.executeQuery("Select *from melodii");
  166. } catch (SQLException e1) {
  167. // TODO Auto-generated catch block
  168. e1.printStackTrace();
  169. }
  170. }
  171. }
  172. }
  173.  
  174. }
  175.  
  176. public class MainApp {
  177.  
  178. public static void main(String[] args) throws HeadlessException, SQLException {
  179. //JFrame f = new Window1("Restaurante");
  180. JFrame f=new Window2("Melodii");
  181. f.setVisible(true);
  182. }
  183.  
  184. }
  185.  
  186.  
  187.  
  188.  
  189. //frame
  190.  
  191.  
  192. package testlab;
  193.  
  194. import java.awt.Dimension;
  195. import java.awt.FlowLayout;
  196. import java.awt.GraphicsConfiguration;
  197. import java.awt.GridLayout;
  198. import java.awt.HeadlessException;
  199. import java.awt.event.ActionEvent;
  200. import java.awt.event.ActionListener;
  201. import java.util.ArrayList;
  202. import java.util.Collections;
  203. import java.util.Comparator;
  204.  
  205. import javax.swing.DefaultListModel;
  206. import javax.swing.JButton;
  207. import javax.swing.JFrame;
  208. import javax.swing.JLabel;
  209. import javax.swing.JList;
  210. import javax.swing.JPanel;
  211. import javax.swing.JTextField;
  212. import javax.swing.ListModel;
  213.  
  214. class Carte
  215. {
  216. String titlu,autor,an;
  217.  
  218. public Carte(String titlu, String autor, String an) {
  219. super();
  220. this.titlu = titlu;
  221. this.autor = autor;
  222. this.an = an;
  223. }
  224.  
  225. @Override
  226. public String toString() {
  227. return titlu + ", " + autor + ", " + an;
  228. }
  229.  
  230.  
  231. //getters and setters
  232. public String getTitlu() {
  233. return titlu;
  234. }
  235.  
  236. public void setTitlu(String titlu) {
  237. this.titlu = titlu;
  238. }
  239.  
  240. public String getAutor() {
  241. return autor;
  242. }
  243.  
  244. public void setAutor(String autor) {
  245. this.autor = autor;
  246. }
  247.  
  248. public String getAn() {
  249. return an;
  250. }
  251.  
  252. public void setAn(String an) {
  253. this.an = an;
  254. }
  255.  
  256. }
  257.  
  258. class Window extends JFrame
  259. {
  260. static JTextField txttitlu,txtautor,txtan;
  261. static JLabel lblTitlu,lblAutor,lblAn;
  262. static JButton btnAdaugare,btnAfisare,btnAfisareOrd;
  263. static JList lista;
  264. static ArrayList<Carte> carti = new ArrayList<Carte>();
  265. static DefaultListModel listM = new DefaultListModel<>();
  266. public Window(String title) throws HeadlessException {
  267. super(title);
  268. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  269.  
  270. getContentPane().setLayout(new GridLayout(5, 1));
  271. setSize(400,400);
  272.  
  273. //instantiate
  274. txttitlu = new JTextField(20);
  275. txtautor = new JTextField(20);
  276. txtan = new JTextField(20);
  277.  
  278. lblTitlu= new JLabel("Titlu");
  279. lblAutor= new JLabel("Autor");
  280. lblAn = new JLabel("An");
  281.  
  282. //subpanel
  283. JPanel firstRow = new JPanel(new FlowLayout());
  284. firstRow.add(lblTitlu);
  285. firstRow.add(txttitlu);
  286. JPanel secondRow = new JPanel(new FlowLayout());
  287. secondRow.add(lblAutor);
  288. secondRow.add(txtautor);
  289. JPanel thirdRow = new JPanel(new FlowLayout());
  290. thirdRow.add(lblAn);
  291. thirdRow.add(txtan);
  292.  
  293. getContentPane().add(firstRow);
  294. getContentPane().add(secondRow);
  295. getContentPane().add(thirdRow);
  296.  
  297. //buttons
  298. btnAdaugare = new JButton("Adaugare");
  299. btnAfisare = new JButton("Afisare");
  300. btnAfisareOrd = new JButton("Afisare orodnata");
  301.  
  302. JPanel fourthRow = new JPanel(new FlowLayout());
  303. fourthRow.add(btnAdaugare);
  304. fourthRow.add(btnAfisare);
  305. fourthRow.add(btnAfisareOrd);
  306. getContentPane().add(fourthRow);
  307.  
  308.  
  309.  
  310. lista = new JList(listM);
  311. lista.setPreferredSize(new Dimension(380,100));
  312.  
  313. //adding listeners
  314. btnAdaugare.addActionListener(new Clicked());
  315. btnAfisare.addActionListener(new Clicked());
  316. btnAfisareOrd.addActionListener(new Clicked());
  317. //adding fifthRow
  318. JPanel fifthRow = new JPanel(new FlowLayout());
  319. fifthRow.add(lista);
  320. getContentPane().add(fifthRow);
  321.  
  322. }
  323.  
  324. }
  325.  
  326. class Clicked implements ActionListener
  327. {
  328.  
  329. @Override
  330. public void actionPerformed(ActionEvent arg0) {
  331. if(arg0.getSource().equals(Window.btnAdaugare))
  332. {
  333. Window.carti.add(new Carte(Window.txttitlu.getText(),Window.txtautor.getText(),Window.txtan.getText()));
  334. }
  335. if(arg0.getSource().equals(Window.btnAfisare))
  336. {
  337. Window.listM.clear();
  338. for(Carte c : Window.carti)
  339. {
  340. Window.listM.addElement(c.toString());
  341. System.out.println(c.toString());
  342. }
  343. }
  344. if(arg0.getSource().equals(Window.btnAfisareOrd))
  345. {
  346. Window.listM.clear();
  347. Collections.sort(Window.carti, new Compare());
  348. for(Carte c : Window.carti)
  349. {
  350. Window.listM.addElement(c.toString());
  351. System.out.println(c.toString());
  352. }
  353. }
  354. }
  355.  
  356. }
  357.  
  358. class Compare implements Comparator<Carte>
  359. {
  360.  
  361. @Override
  362. public int compare(Carte c1, Carte c2) {
  363. return c1.titlu.compareToIgnoreCase(c2.titlu);
  364. }
  365.  
  366. }
  367.  
  368. public class MainApp {
  369.  
  370. public static void main(String[] args) {
  371. JFrame f = new Window("Carti");
  372. f.setVisible(true);
  373. }
  374.  
  375. }
  376.  
  377.  
  378. //html
  379.  
  380.  
  381. <html>
  382. <head>
  383. <title>
  384. Suma a doua numere
  385. </title>
  386. </head>
  387. <body>
  388. <form method ="GET" action="http://localhost:8080/Proiect1/Calcul">
  389. <p>Numarul 1:<input type="text" name="nr1"></p>
  390. <p>Numarul 2:<input type="text" name="nr2"></p>
  391. <p><input type="submit" value ="Trimite" ></p>
  392. </form>
  393. </body>
  394. </html>
  395.  
  396.  
  397.  
  398.  
  399.  
  400. //Servlet
  401.  
  402.  
  403.  
  404.  
  405. import java.io.IOException;
  406. import java.io.PrintWriter;
  407. import java.util.ArrayList;
  408. import java.util.Calendar;
  409. import java.util.Collections;
  410. import java.util.Comparator;
  411.  
  412. import javax.servlet.ServletException;
  413. import javax.servlet.annotation.WebServlet;
  414. import javax.servlet.http.HttpServlet;
  415. import javax.servlet.http.HttpServletRequest;
  416. import javax.servlet.http.HttpServletResponse;
  417.  
  418.  
  419.  
  420. class Destinatie
  421. {
  422. String denumire;
  423. Calendar data_vizita;
  424. public Destinatie(String denumire,Calendar data_vizita) {
  425. super();
  426. this.denumire = denumire;
  427. this.data_vizita = data_vizita;
  428. }
  429. public String getDenumire() {
  430. return denumire;
  431. }
  432. public void setDenumire(String denumire) {
  433. this.denumire = denumire;
  434. }
  435. public Calendar getData_vizita() {
  436. return data_vizita;
  437. }
  438. public void setData_vizita(Calendar data_vizita) {
  439. this.data_vizita = data_vizita;
  440. }
  441. public String getData()
  442. {
  443. return data_vizita.get(Calendar.DAY_OF_MONTH) + "/" + data_vizita.get(Calendar.MONTH) +"/" + data_vizita.get(Calendar.YEAR);
  444. }
  445. }
  446.  
  447. class ComparaDest implements Comparator<Destinatie>
  448. {
  449.  
  450. @Override
  451. public int compare(Destinatie d1, Destinatie d2) {
  452. return d1.getDenumire().compareToIgnoreCase(d2.denumire);
  453. }
  454.  
  455. }
  456. /**
  457. * Servlet implementation class Turism
  458. */
  459. @WebServlet("/Turism")
  460. public class Turism extends HttpServlet {
  461. private static final long serialVersionUID = 1L;
  462. static ArrayList<Destinatie> destinatii=new ArrayList<Destinatie>();
  463. /**
  464. * @see HttpServlet#HttpServlet()
  465. */
  466. public Turism() {
  467. super();
  468. // TODO Auto-generated constructor stub
  469. }
  470.  
  471. /**
  472. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  473. */
  474. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  475. PrintWriter out= response.getWriter();
  476. if(request.getParameter("btnAdaugare")!=null)
  477. {
  478. String destinatie=request.getParameter("denumire");
  479. String zi=request.getParameter("zi");
  480. String luna=request.getParameter("luna");
  481. String an=request.getParameter("an");
  482.  
  483. Calendar data= Calendar.getInstance();
  484.  
  485. data.set(Integer.parseInt(an),Integer.parseInt(luna), Integer.parseInt(zi));
  486. destinatii.add(new Destinatie(destinatie,data));
  487.  
  488.  
  489.  
  490. out.println("<html><head></head><body>");
  491. out.println("<table border='2' align='center'><tr><th>Denumire</th><th>Data</th></tr>");
  492. for(Destinatie d : destinatii)
  493. {
  494. out.println("<tr><td>"+ d.getDenumire()+"</td><td>"+ d.getData()+"</td></tr>");
  495. }
  496. out.println("</table>");
  497. out.println("<form method='get' action='Turism'>");
  498. out.println("<input type='submit' name='btnSort' value='Sorteaza'>");
  499. out.println("</form>");
  500. }
  501.  
  502. if(request.getParameter("btnSort") != null)
  503. {
  504. out.println("<table border='2' align='center'><tr><th>Denumire</th><th>Data</th></tr>");
  505. System.out.println("Buton de sortare-------------------------!");
  506. Collections.sort(destinatii,new ComparaDest());
  507. for(Destinatie d : destinatii)
  508. {
  509. System.out.println(d.getDenumire());
  510. out.println("<tr><td>"+ d.getDenumire() +"</td><td>" + d.getData()+"</td></tr>");
  511. }
  512. out.println("</table>");
  513. }
  514. out.println("</body></html>");
  515. }
  516.  
  517.  
  518. /**
  519. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  520. */
  521. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  522. {
  523. doGet(request,response);
  524. }
  525. }
  526. }
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533. Servlet+baza de date
  534.  
  535.  
  536.  
  537. packageexemplu_JDBC;
  538. importjava.io.*;
  539. importjavax.servlet.annotation.WebServlet;
  540. importjavax.servlet.http.*;
  541. importjava.sql.*;
  542. importcom.mysql.jdbc.Statement;
  543.  
  544. @WebServlet("/OperatiiJDBC")
  545.  
  546. public class OperatiiJDBC extends HttpServlet {
  547. private static final long serialVersionUID = 1L;
  548. protected void doGet(HttpServletRequest request, HttpServletResponse response){
  549. try{
  550. String url= "jdbc:mysql://localhost:3306/test";
  551. Statement sql=null;
  552. ResultSet rs=null;
  553. Class.forName("com.mysql.jdbc.Driver").newInstance ();
  554. Connection con= DriverManager.getConnection(url,"root","root");
  555. sql= (Statement) con.createStatement();
  556. rs= sql.executeQuery("select * from persoane");
  557. PrintWriter out=response.getWriter();
  558. out.println("<html><head><title>Persoane</title></head><body>");
  559. out.println("<form method='get' action='OperatiiJDBC'>");
  560. out.println("<table align='center' width='50%' cellspacing='5'>");
  561. out.println("<tr><td align='right'>Id</td><td><input type='text' name='txtId'"
  562. + " size='5'></td></tr>");
  563. out.println("<tr><td align='right'>Nume</td><td><input type='text'"
  564. + " name='txtNume'></td></tr>");
  565. out.println("<tr><td align='right'>Varsta</td><td><input type='text'"
  566. + " name='txtVarsta' size='5' maxlength='3'></td></tr>");
  567. out.println("<tr><td colspan='2' align='center'><input type='submit' "
  568. + " name='btnAdauga' value='Adauga' style='width: 110px; height: 25px;'>");
  569. out.println("<input type='submit' name='btnModifica' value='Modifica' "
  570. + " style='width: 110px; height: 25px;'>");
  571. out.println("<input type='submit' name='btnSterge' value='Sterge'"
  572. + " style='width: 110px; height: 25px;'></td></tr>");
  573. out.println("</table></form>");
  574.  
  575. out.println("<table align='center' width='50%' border='1'>");
  576. out.println("<tr><th>Id</th><th>Nume</th><th>Varsta</th></tr>");
  577. if(request.getParameter("btnAdauga")!=null){
  578. int id=Integer.parseInt(request.getParameter("txtId"));
  579. String nume=request.getParameter("txtNume");
  580. int varsta=Integer.parseInt(request.getParameter("txtVarsta"));
  581. String comanda="insert into persoane values ("+id+",'"+nume+"',"+varsta+")";
  582. try{
  583. sql.executeUpdate(comanda);
  584. rs= sql.executeQuery("select * from persoane");
  585. }
  586. catch(SQLException e) {
  587. System.out.println(comanda+"\n"+e);
  588. }
  589. }
  590. if(request.getParameter("btnModifica")!=null){
  591. int id=Integer.parseInt(request.getParameter("txtId"));
  592. String nume=request.getParameter("txtNume");
  593. int varsta=Integer.parseInt(request.getParameter("txtVarsta"));
  594. String comanda="update persoane set nume='"+nume+"', varsta="+varsta+" where id="+id;
  595. try{
  596. sql.executeUpdate(comanda);
  597. rs= sql.executeQuery("select * from persoane");
  598. }
  599. catch(SQLException e) {
  600. System.out.println(comanda+"\n"+e);
  601. }
  602. }
  603.  
  604.  
  605.  
  606.  
  607. if(request.getParameter("btnSterge")!=null){
  608. int id=Integer.parseInt(request.getParameter("txtId"));
  609. String comanda="delete from persoane where id="+id;
  610. try{
  611. sql.executeUpdate(comanda);
  612. rs= sql.executeQuery("select * from persoane");
  613. }
  614. catch(SQLException e) {
  615. System.out.println(comanda+"\n"+e);
  616. }
  617. }
  618. while(rs.next())
  619. out.println("<tr><td>"+rs.getInt("Id")+"</td><td>"+ rs.getString("nume")
  620. + "</td><td>"+rs.getInt(3)+"</td><tr>");
  621. out.println("</table></body></html>");
  622. }
  623. catch(Exception ex){
  624. System.out.println(ex);
  625. }
  626. }
  627. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement