Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //baze de date
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.HeadlessException;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Vector;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- class Restaurant
- {
- String restaurant,specific,zona;
- public Restaurant(String restaurant, String specific, String zona) {
- super();
- this.restaurant = restaurant;
- this.specific = specific;
- this.zona = zona;
- }
- public String getRestaurant() {
- return restaurant;
- }
- public void setRestaurant(String restaurant) {
- this.restaurant = restaurant;
- }
- public String getSpecific() {
- return specific;
- }
- public void setSpecific(String specific) {
- this.specific = specific;
- }
- public String getZona() {
- return zona;
- }
- public void setZona(String zona) {
- this.zona = zona;
- }
- @Override
- public String toString() {
- return "Restaurant=" + restaurant + ", specific=" + specific + ", zona=" + zona;
- }
- }
- class Window1 extends JFrame
- {
- static Vector restaurante=new Vector();
- static JList lstRestaurante;
- static DefaultListModel listM;
- public Window1(String title) throws HeadlessException, SQLException {
- super(title);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- getContentPane().setLayout(new BorderLayout());
- setSize(400,400);
- listM=new DefaultListModel<>();
- lstRestaurante= new JList(listM);
- String url = "jdbc:mysql://localhost:3306/test";
- Connection con=DriverManager.getConnection(url, "root", "root");
- Statement stmt = (Statement)con.createStatement();
- ResultSet rs=stmt.executeQuery("Select *from restaurante");
- JLabel titlu=new JLabel("Lista Restaurante");
- getContentPane().add(titlu, BorderLayout.NORTH);
- getContentPane().add(lstRestaurante,BorderLayout.CENTER);
- rs.first();
- while(rs.next())
- {
- Restaurant res=new Restaurant(rs.getString(1),rs.getString(2),rs.getString(3));
- restaurante.add(res);
- }
- for(int i=0;i<restaurante.size();i++)
- {
- Restaurant r = (Restaurant) restaurante.elementAt(i);
- listM.addElement(r.toString());
- }
- }
- }
- class Window2 extends JFrame
- {
- static Vector melodii=new Vector();
- static JButton btnSterge= new JButton("Sterge");
- static JLabel melodie=new JLabel("Melodie");
- static JTextField txtMelodie= new JTextField(15);
- static DefaultListModel listMod=new DefaultListModel<>();
- static JList lstMelodii=new JList(listMod);
- static Connection con;
- static Statement stmt;
- static ResultSet rs;
- public Window2(String title) throws HeadlessException, SQLException {
- super(title);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- getContentPane().setLayout(new GridLayout(2,1));
- setSize(400,400);
- JPanel panel1 = new JPanel(new FlowLayout());
- panel1.add(melodie);
- panel1.add(txtMelodie);
- panel1.add(btnSterge);
- String url = "jdbc:mysql://localhost:3306/test";
- con=DriverManager.getConnection(url, "root", "root");
- stmt = (Statement)con.createStatement();
- rs=stmt.executeQuery("Select *from melodii");
- getContentPane().add(panel1);
- getContentPane().add(lstMelodii);
- rs.beforeFirst();
- while(rs.next()) {
- melodii.add(rs.getString(1));
- }
- for(int i=0;i<melodii.size();i++)
- {
- listMod.addElement(melodii.elementAt(i));
- }
- btnSterge.addActionListener(new Clicked());
- }
- }
- class Clicked implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent e) {
- String melodieS=Window2.txtMelodie.getText();
- for(int i=0;i<Window2.melodii.size();i++)
- {
- if(Window2.melodii.elementAt(i).equals(melodieS))
- {
- Window2.melodii.remove(i);
- Window2.listMod.removeElementAt(i);
- try {
- PreparedStatement deleteStmt=Window2.con.prepareStatement("Delete from melodii where melodie=?");
- deleteStmt.setString(1, melodieS);
- deleteStmt.execute();
- Window2.rs=Window2.stmt.executeQuery("Select *from melodii");
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
- }
- public class MainApp {
- public static void main(String[] args) throws HeadlessException, SQLException {
- //JFrame f = new Window1("Restaurante");
- JFrame f=new Window2("Melodii");
- f.setVisible(true);
- }
- }
- //frame
- package testlab;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.GraphicsConfiguration;
- import java.awt.GridLayout;
- import java.awt.HeadlessException;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.ListModel;
- class Carte
- {
- String titlu,autor,an;
- public Carte(String titlu, String autor, String an) {
- super();
- this.titlu = titlu;
- this.autor = autor;
- this.an = an;
- }
- @Override
- public String toString() {
- return titlu + ", " + autor + ", " + an;
- }
- //getters and setters
- public String getTitlu() {
- return titlu;
- }
- public void setTitlu(String titlu) {
- this.titlu = titlu;
- }
- public String getAutor() {
- return autor;
- }
- public void setAutor(String autor) {
- this.autor = autor;
- }
- public String getAn() {
- return an;
- }
- public void setAn(String an) {
- this.an = an;
- }
- }
- class Window extends JFrame
- {
- static JTextField txttitlu,txtautor,txtan;
- static JLabel lblTitlu,lblAutor,lblAn;
- static JButton btnAdaugare,btnAfisare,btnAfisareOrd;
- static JList lista;
- static ArrayList<Carte> carti = new ArrayList<Carte>();
- static DefaultListModel listM = new DefaultListModel<>();
- public Window(String title) throws HeadlessException {
- super(title);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- getContentPane().setLayout(new GridLayout(5, 1));
- setSize(400,400);
- //instantiate
- txttitlu = new JTextField(20);
- txtautor = new JTextField(20);
- txtan = new JTextField(20);
- lblTitlu= new JLabel("Titlu");
- lblAutor= new JLabel("Autor");
- lblAn = new JLabel("An");
- //subpanel
- JPanel firstRow = new JPanel(new FlowLayout());
- firstRow.add(lblTitlu);
- firstRow.add(txttitlu);
- JPanel secondRow = new JPanel(new FlowLayout());
- secondRow.add(lblAutor);
- secondRow.add(txtautor);
- JPanel thirdRow = new JPanel(new FlowLayout());
- thirdRow.add(lblAn);
- thirdRow.add(txtan);
- getContentPane().add(firstRow);
- getContentPane().add(secondRow);
- getContentPane().add(thirdRow);
- //buttons
- btnAdaugare = new JButton("Adaugare");
- btnAfisare = new JButton("Afisare");
- btnAfisareOrd = new JButton("Afisare orodnata");
- JPanel fourthRow = new JPanel(new FlowLayout());
- fourthRow.add(btnAdaugare);
- fourthRow.add(btnAfisare);
- fourthRow.add(btnAfisareOrd);
- getContentPane().add(fourthRow);
- lista = new JList(listM);
- lista.setPreferredSize(new Dimension(380,100));
- //adding listeners
- btnAdaugare.addActionListener(new Clicked());
- btnAfisare.addActionListener(new Clicked());
- btnAfisareOrd.addActionListener(new Clicked());
- //adding fifthRow
- JPanel fifthRow = new JPanel(new FlowLayout());
- fifthRow.add(lista);
- getContentPane().add(fifthRow);
- }
- }
- class Clicked implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- if(arg0.getSource().equals(Window.btnAdaugare))
- {
- Window.carti.add(new Carte(Window.txttitlu.getText(),Window.txtautor.getText(),Window.txtan.getText()));
- }
- if(arg0.getSource().equals(Window.btnAfisare))
- {
- Window.listM.clear();
- for(Carte c : Window.carti)
- {
- Window.listM.addElement(c.toString());
- System.out.println(c.toString());
- }
- }
- if(arg0.getSource().equals(Window.btnAfisareOrd))
- {
- Window.listM.clear();
- Collections.sort(Window.carti, new Compare());
- for(Carte c : Window.carti)
- {
- Window.listM.addElement(c.toString());
- System.out.println(c.toString());
- }
- }
- }
- }
- class Compare implements Comparator<Carte>
- {
- @Override
- public int compare(Carte c1, Carte c2) {
- return c1.titlu.compareToIgnoreCase(c2.titlu);
- }
- }
- public class MainApp {
- public static void main(String[] args) {
- JFrame f = new Window("Carti");
- f.setVisible(true);
- }
- }
- //html
- <html>
- <head>
- <title>
- Suma a doua numere
- </title>
- </head>
- <body>
- <form method ="GET" action="http://localhost:8080/Proiect1/Calcul">
- <p>Numarul 1:<input type="text" name="nr1"></p>
- <p>Numarul 2:<input type="text" name="nr2"></p>
- <p><input type="submit" value ="Trimite" ></p>
- </form>
- </body>
- </html>
- //Servlet
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Collections;
- import java.util.Comparator;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- class Destinatie
- {
- String denumire;
- Calendar data_vizita;
- public Destinatie(String denumire,Calendar data_vizita) {
- super();
- this.denumire = denumire;
- this.data_vizita = data_vizita;
- }
- public String getDenumire() {
- return denumire;
- }
- public void setDenumire(String denumire) {
- this.denumire = denumire;
- }
- public Calendar getData_vizita() {
- return data_vizita;
- }
- public void setData_vizita(Calendar data_vizita) {
- this.data_vizita = data_vizita;
- }
- public String getData()
- {
- return data_vizita.get(Calendar.DAY_OF_MONTH) + "/" + data_vizita.get(Calendar.MONTH) +"/" + data_vizita.get(Calendar.YEAR);
- }
- }
- class ComparaDest implements Comparator<Destinatie>
- {
- @Override
- public int compare(Destinatie d1, Destinatie d2) {
- return d1.getDenumire().compareToIgnoreCase(d2.denumire);
- }
- }
- /**
- * Servlet implementation class Turism
- */
- @WebServlet("/Turism")
- public class Turism extends HttpServlet {
- private static final long serialVersionUID = 1L;
- static ArrayList<Destinatie> destinatii=new ArrayList<Destinatie>();
- /**
- * @see HttpServlet#HttpServlet()
- */
- public Turism() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- PrintWriter out= response.getWriter();
- if(request.getParameter("btnAdaugare")!=null)
- {
- String destinatie=request.getParameter("denumire");
- String zi=request.getParameter("zi");
- String luna=request.getParameter("luna");
- String an=request.getParameter("an");
- Calendar data= Calendar.getInstance();
- data.set(Integer.parseInt(an),Integer.parseInt(luna), Integer.parseInt(zi));
- destinatii.add(new Destinatie(destinatie,data));
- out.println("<html><head></head><body>");
- out.println("<table border='2' align='center'><tr><th>Denumire</th><th>Data</th></tr>");
- for(Destinatie d : destinatii)
- {
- out.println("<tr><td>"+ d.getDenumire()+"</td><td>"+ d.getData()+"</td></tr>");
- }
- out.println("</table>");
- out.println("<form method='get' action='Turism'>");
- out.println("<input type='submit' name='btnSort' value='Sorteaza'>");
- out.println("</form>");
- }
- if(request.getParameter("btnSort") != null)
- {
- out.println("<table border='2' align='center'><tr><th>Denumire</th><th>Data</th></tr>");
- System.out.println("Buton de sortare-------------------------!");
- Collections.sort(destinatii,new ComparaDest());
- for(Destinatie d : destinatii)
- {
- System.out.println(d.getDenumire());
- out.println("<tr><td>"+ d.getDenumire() +"</td><td>" + d.getData()+"</td></tr>");
- }
- out.println("</table>");
- }
- out.println("</body></html>");
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- {
- doGet(request,response);
- }
- }
- }
- Servlet+baza de date
- packageexemplu_JDBC;
- importjava.io.*;
- importjavax.servlet.annotation.WebServlet;
- importjavax.servlet.http.*;
- importjava.sql.*;
- importcom.mysql.jdbc.Statement;
- @WebServlet("/OperatiiJDBC")
- public class OperatiiJDBC extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response){
- try{
- String url= "jdbc:mysql://localhost:3306/test";
- Statement sql=null;
- ResultSet rs=null;
- Class.forName("com.mysql.jdbc.Driver").newInstance ();
- Connection con= DriverManager.getConnection(url,"root","root");
- sql= (Statement) con.createStatement();
- rs= sql.executeQuery("select * from persoane");
- PrintWriter out=response.getWriter();
- out.println("<html><head><title>Persoane</title></head><body>");
- out.println("<form method='get' action='OperatiiJDBC'>");
- out.println("<table align='center' width='50%' cellspacing='5'>");
- out.println("<tr><td align='right'>Id</td><td><input type='text' name='txtId'"
- + " size='5'></td></tr>");
- out.println("<tr><td align='right'>Nume</td><td><input type='text'"
- + " name='txtNume'></td></tr>");
- out.println("<tr><td align='right'>Varsta</td><td><input type='text'"
- + " name='txtVarsta' size='5' maxlength='3'></td></tr>");
- out.println("<tr><td colspan='2' align='center'><input type='submit' "
- + " name='btnAdauga' value='Adauga' style='width: 110px; height: 25px;'>");
- out.println("<input type='submit' name='btnModifica' value='Modifica' "
- + " style='width: 110px; height: 25px;'>");
- out.println("<input type='submit' name='btnSterge' value='Sterge'"
- + " style='width: 110px; height: 25px;'></td></tr>");
- out.println("</table></form>");
- out.println("<table align='center' width='50%' border='1'>");
- out.println("<tr><th>Id</th><th>Nume</th><th>Varsta</th></tr>");
- if(request.getParameter("btnAdauga")!=null){
- int id=Integer.parseInt(request.getParameter("txtId"));
- String nume=request.getParameter("txtNume");
- int varsta=Integer.parseInt(request.getParameter("txtVarsta"));
- String comanda="insert into persoane values ("+id+",'"+nume+"',"+varsta+")";
- try{
- sql.executeUpdate(comanda);
- rs= sql.executeQuery("select * from persoane");
- }
- catch(SQLException e) {
- System.out.println(comanda+"\n"+e);
- }
- }
- if(request.getParameter("btnModifica")!=null){
- int id=Integer.parseInt(request.getParameter("txtId"));
- String nume=request.getParameter("txtNume");
- int varsta=Integer.parseInt(request.getParameter("txtVarsta"));
- String comanda="update persoane set nume='"+nume+"', varsta="+varsta+" where id="+id;
- try{
- sql.executeUpdate(comanda);
- rs= sql.executeQuery("select * from persoane");
- }
- catch(SQLException e) {
- System.out.println(comanda+"\n"+e);
- }
- }
- if(request.getParameter("btnSterge")!=null){
- int id=Integer.parseInt(request.getParameter("txtId"));
- String comanda="delete from persoane where id="+id;
- try{
- sql.executeUpdate(comanda);
- rs= sql.executeQuery("select * from persoane");
- }
- catch(SQLException e) {
- System.out.println(comanda+"\n"+e);
- }
- }
- while(rs.next())
- out.println("<tr><td>"+rs.getInt("Id")+"</td><td>"+ rs.getString("nume")
- + "</td><td>"+rs.getInt(3)+"</td><tr>");
- out.println("</table></body></html>");
- }
- catch(Exception ex){
- System.out.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement