Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.io.Serializable;
- import java.util.Date;
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class Autoturism implements Serializable{
- @Override
- public String toString() {
- return "Autoturism [nrMasina=" + nrMasina + ", idPermisAuto=" + idPermisAuto + ", pretPerZi=" + pretPerZi
- + ", deCand=" + deCand + ", panaCand=" + panaCand + "]";
- }
- private String nrMasina;
- private String idPermisAuto;
- private float pretPerZi;
- private Date deCand;
- private Date panaCand;
- public String getNrMasina() {
- return nrMasina;
- }
- @XmlAttribute
- public void setNrMasina(String nrMasina) {
- this.nrMasina = nrMasina;
- }
- public String getIdPermisAuto() {
- return idPermisAuto;
- }
- @XmlElement
- public void setIdPermisAuto(String idPermisAuto) {
- if(idPermisAuto !=null)
- {
- this.idPermisAuto = idPermisAuto;
- }
- else
- idPermisAuto="NIMIC";
- }
- public float getPretPerZi() {
- return pretPerZi;
- }
- @XmlElement
- public void setPretPerZi(float pretPerZi) {
- this.pretPerZi = pretPerZi;
- }
- public Date getDeCand() {
- return deCand;
- }
- @XmlElement
- public void setDeCand(Date deCand) {
- if(deCand!=null)
- {
- this.deCand = deCand;
- }
- }
- public Date getPanaCand() {
- return panaCand;
- }
- @XmlElement
- public void setPanaCand(Date panaCand) {
- if(panaCand!=null)
- {
- this.panaCand = panaCand;
- }
- }
- public Autoturism(String nrMasina, String idPermisAuto, float pretPerZi, Date deCand, Date panaCand) {
- super();
- this.nrMasina = nrMasina;
- this.idPermisAuto = idPermisAuto;
- this.pretPerZi = pretPerZi;
- this.deCand = deCand;
- this.panaCand = panaCand;
- }
- public Autoturism() {
- super();
- }
- }
- package application;
- import java.util.Date;
- public class Inchiriere {
- private String idPermisAuto;
- private Date deCand;
- public Inchiriere(String idPermisAuto, Date deCand, Date panaCand) {
- super();
- this.idPermisAuto = idPermisAuto;
- this.deCand = deCand;
- this.panaCand = panaCand;
- }
- public String getIdPermisAuto() {
- return idPermisAuto;
- }
- public void setIdPermisAuto(String idPermisAuto) {
- this.idPermisAuto = idPermisAuto;
- }
- public Date getDeCand() {
- return deCand;
- }
- public void setDeCand(Date deCand) {
- this.deCand = deCand;
- }
- public Date getPanaCand() {
- return panaCand;
- }
- public void setPanaCand(Date panaCand) {
- this.panaCand = panaCand;
- }
- private Date panaCand;
- public float getPretInchiriere(Autoturism a)
- {
- return a.getPretPerZi();
- }
- }
- package application;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.LinkedList;
- import java.util.List;
- public class Client implements Runnable{
- public static List<Autoturism>autoturisme=new LinkedList<Autoturism>();
- public static List<Inchiriere>inchiriate= new LinkedList<Inchiriere>();
- private Socket socket=null;
- private ObjectInputStream in=null;
- @Override
- public void run() {
- try {
- socket= new Socket("localhost",7000);
- in=new ObjectInputStream(socket.getInputStream());
- System.out.println("s-a conectat");
- while(true)
- {
- autoturisme=(List<Autoturism>) in.readObject();
- for(Autoturism a : autoturisme)
- {
- Inchiriere inch= new Inchiriere(a.getIdPermisAuto(),a.getDeCand(),a.getPanaCand());
- inchiriate.add(inch);
- System.out.println(a);
- }
- }
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // finally {
- // try {
- // in.close();
- // socket.close();
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- // }
- }
- public Client(Socket socket, ObjectInputStream in) {
- super();
- this.socket = socket;
- this.in = in;
- }
- public Client() {
- // TODO Auto-generated constructor stub
- }
- public static void main(String[] args) {
- Client c= new Client();
- Thread t = new Thread(c);
- t.start();
- }
- }
- package application;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- 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.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.LinkedList;
- import java.util.List;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.annotation.XmlAccessorType;
- public class Server implements Runnable{
- public static List<Autoturism>auto= new ArrayList<Autoturism>();
- private ServerSocket server= null;
- private Socket socket=null;
- private ObjectOutputStream out=null;
- public Server() {
- super();
- Citeste();
- //bazaDate();
- mozilla();
- Xml();
- }
- public void mozilla()
- {
- try {
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\mihai1\\Documents\\Eclipse\\Auto.sqlite");
- Statement st =conn.createStatement();
- //st.execute("create table Auto(nrMasina string,idPermisAuto string,pretPerZi float,deCand string,panaCand string)");
- for(Autoturism a : auto)
- {
- st.execute("insert into Auto(nrMasini,idPermisAuto,pretPerZi,deCand,panaCand)"+"values('"+a.getNrMasina()+"','"+a.getIdPermisAuto()+"',"+a.getPretPerZi()+",'"+a.getDeCand().toString()+"','"+a.getPanaCand().toString()+"')");
- }
- st.close();
- conn.close();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void bazaDate()
- {
- try {
- Class.forName("org.sqlite.JDBC");
- Connection con= DriverManager.getConnection("jdbc:sqlite:Masini.db");
- Statement st =con.createStatement();
- st.execute("create table Masini(nrMasina string,idPermisAuto string,pretPerZi float,deCand string,panaCand string)");
- for(Autoturism a : auto)
- {
- st.execute("insert into Masini(nrMasina,idPermisAuto,pretPerZi,deCand,panaCand)"+"values('"+a.getNrMasina()+"','"+a.getIdPermisAuto()+"',"+a.getPretPerZi()+",'"+a.getDeCand().toString()+"','"+a.getPanaCand().toString()+"')");
- }
- String comanda="select * from Masini";
- ResultSet rs=st.executeQuery(comanda);
- while(rs.next())
- {
- String nr=rs.getString("nrMasina");
- String id=rs.getString("idPermisAuto");
- float pret=rs.getInt("pretPerZi");
- String cand=rs.getString("deCand");
- String pana=rs.getString("panaCand");
- System.out.println(nr);
- System.out.println(id);
- System.out.println(pret);
- System.out.println(cand);
- System.out.println(pana);
- }
- rs.close();
- con.close();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void Citeste()
- {
- File f = new File("Auto.txt");
- try {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String linie=br.readLine();
- while(linie!=null)
- {
- String[] ex=linie.split(",");
- String numar=ex[0];
- String id=ex[1];
- float pret=Float.parseFloat(ex[2]);
- SimpleDateFormat data= new SimpleDateFormat("dd/mm/yyyy");
- Date de=data.parse(ex[3]);
- Date pana=data.parse(ex[4]);
- Autoturism at= new Autoturism(numar,id,pret,de,pana);
- auto.add(at);
- linie=br.readLine();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void Xml()
- {
- File f = new File("Xml.xml");
- try {
- JAXBContext con=JAXBContext.newInstance(ListaAuto.class);
- Marshaller mar=con.createMarshaller();
- mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- ListaAuto q= new ListaAuto();
- q.setLst(auto);
- mar.marshal(q, f);
- //mar.marshal(auto, System.out);
- } catch (JAXBException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- try {
- server=new ServerSocket(7000);
- socket=server.accept();
- out= new ObjectOutputStream(socket.getOutputStream());
- System.out.println("merge");
- out.writeObject(auto);
- while(true)
- {
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // finally {
- // try {
- // out.close();
- // socket.close();
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- //
- // }
- }
- public static void main(String[] args) {
- Server ser= new Server();
- Thread tt = new Thread(ser);
- tt.start();
- }
- }
- package application;
- import java.util.List;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class ListaAuto {
- private List<Autoturism> lst= null;
- public List<Autoturism> getLst() {
- return lst;
- }
- @XmlElement
- public void setLst(List<Autoturism> lst) {
- this.lst = lst;
- }
- }
- -----------------------------------------------------------------------
- package application;
- import java.io.Serializable;
- public class Student implements Serializable{
- private int varsta;
- private String nume;
- public int getVarsta() {
- return varsta;
- }
- public void setVarsta(int varsta) {
- this.varsta = varsta;
- }
- public String getNume() {
- return nume;
- }
- public void setNume(String nume) {
- this.nume = nume;
- }
- public Student(int varsta, String nume) {
- super();
- this.varsta = varsta;
- this.nume = nume;
- }
- @Override
- public String toString() {
- return "Student [varsta=" + varsta + ", nume=" + nume + "]";
- }
- public Student() {
- super();
- }
- }
- package application;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.LinkedList;
- import java.util.List;
- public class Client {
- public static List<Student>lista = new LinkedList<Student>();
- private Socket s=null;
- private ObjectOutputStream out=null;
- private boolean conectat=false;
- public Client() {
- super();
- }
- public void comunicare()
- {
- while(!conectat)
- {
- try {
- s= new Socket("localhost",7001);
- System.out.println("conectat");
- conectat=true;
- File f = new File("Studenti.txt");
- FileReader fr= new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String line=br.readLine();
- while(line!=null)
- {
- int id=Integer.parseInt(line);
- line=br.readLine();
- String n=line;
- line=br.readLine();
- Student stud= new Student(id,n);
- lista.add(stud);
- }
- out=new ObjectOutputStream(s.getOutputStream());
- //Student student= new Student(21,"Puia");
- out.writeObject(lista);
- out.close();
- s.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- Client c= new Client();
- c.comunicare();
- }
- }
- package application;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.LinkedList;
- import java.util.List;
- import jdk.jfr.events.FileWriteEvent;
- public class Server {
- public static List<Student>lista= new LinkedList<Student>();
- public static List<Student>studenti= new LinkedList<Student>();
- private ServerSocket ss=null;
- private Socket socket=null;
- private ObjectInputStream in=null;
- public Server() {
- super();
- }
- public void comunicare()
- {
- try {
- ss=new ServerSocket(7001);
- socket=ss.accept();
- in=new ObjectInputStream(socket.getInputStream());
- //Student student= (Student) in.readObject();
- lista=(List<Student>) in.readObject();
- for(Student q : lista)
- {
- System.out.println("obiectul trimis: "+ q);
- }
- File f = new File("fis.txt");
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- for(Student sss : lista)
- {
- bw.write(sss.toString());
- bw.newLine();
- }
- FileOutputStream fos = new FileOutputStream("binar.dat");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(lista);
- System.out.println("-----------");
- FileInputStream fis= new FileInputStream("binar.dat");
- ObjectInputStream ois= new ObjectInputStream(fis);
- studenti=(List<Student>) ois.readObject();
- System.out.println(studenti);
- bw.close();
- fw.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Server server=new Server();
- server.comunicare();
- }
- }
- ------------------------------------------------------------------------
- package application;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.LinkedList;
- import java.util.List;
- import javax.xml.bind.ParseConversionEvent;
- import jdk.jfr.events.FileWriteEvent;
- public class Client implements Runnable{
- private Socket socket=null;
- private ObjectOutputStream out=null;
- public static List<Persoana>persoane=new LinkedList<Persoana>();
- public Client(Socket socket, ObjectOutputStream out) {
- super();
- this.socket = socket;
- this.out = out;
- }
- public void citire()
- {
- File f = new File("fis.txt");
- try {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String line=br.readLine();
- while(line!=null)
- {
- String[] ex=line.split(",");
- int v=Integer.parseInt(ex[0]);
- String n=ex[1];
- Persoana p = new Persoana(v,n);
- persoane.add(p);
- //System.out.println(p);
- line=br.readLine();
- }
- br.close();
- fr.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public Client() {
- super();
- citire();
- }
- @Override
- public void run() {
- try {
- socket=new Socket("localhost",5000);
- out=new ObjectOutputStream(socket.getOutputStream());
- out.writeObject(persoane);
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- while(true)
- {
- }
- }
- public static void main(String[] args) {
- Client c= new Client();
- Thread t1 = new Thread(c);
- t1.start();
- }
- }
- package application;
- import java.io.File;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.LinkedList;
- import java.util.List;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- public class Server implements Runnable{
- public static List<Persoana>pers= new LinkedList<Persoana>();
- public static List<Persoana>ppp=new LinkedList<Persoana>();
- private ServerSocket server=null;
- private Socket socket=null;
- private ObjectInputStream in=null;
- public Server() {
- super();
- Mars();
- }
- public Server(ServerSocket server, Socket socket, ObjectInputStream in) {
- super();
- this.server = server;
- this.socket = socket;
- this.in = in;
- }
- public void Mars()
- {
- File f = new File("marmar.xml");
- try {
- JAXBContext cont =JAXBContext.newInstance(ListaPersoane.class);
- Marshaller mar=cont.createMarshaller();
- mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- ListaPersoane q=new ListaPersoane();
- q.setListaPers(ppp);
- mar.marshal(q, f);
- } catch (JAXBException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- try {
- server=new ServerSocket(5000);
- socket=server.accept();
- in=new ObjectInputStream(socket.getInputStream());
- while(true)
- {
- pers=(List<Persoana>) in.readObject();
- for(Persoana p : pers)
- {
- System.out.println("Server: "+p);
- Persoana o= new Persoana(p.getVarsta(),p.getNume());
- ppp.add(o);
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public String toString() {
- return "Server [server=" + server + ", socket=" + socket + ", in=" + in + "]";
- }
- public static void main(String[] args) {
- Server s= new Server();
- Thread t2 = new Thread(s);
- t2.start();
- }
- }
- --------------------------------------------------------------------------
- package main;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.sqlite.SQLiteConnection;
- import com.sun.java_cup.internal.runtime.Symbol;
- public class Main extends Application{
- public static void main(String[] args) {
- String querry = "select * from caini";
- Connection conn = clase.SQLiteConnection.conectare();
- try {
- PreparedStatement ps = conn.prepareStatement(querry);
- ResultSet rs=ps.executeQuery();
- while(rs.next()) //cat timp avem un urmator
- {
- System.out.println(rs.getString("nume")+" este un "+rs.getString("rasa"));
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try
- {
- conn.close();
- }
- catch(Exception ex)
- {
- System.out.println("eroare");
- }
- }
- }
- package clase;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class SQLiteConnection {
- Connection conn;
- public static Connection conectare()
- {
- try {
- Class.forName("org.sqlite.JDBC"); //ii specificam JDBC-ul
- Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\mihai1\\Documents\\Student.sqlite"); //se realizeaza conexiunea
- System.out.println("Ne-am conectat");
- return conn; //se returneaza conexiunea
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null; // daca imi intra pe catch sa-mi dea null
- }
- }
- ----------------------------------------------------------
- public class JAXBExample {
- public static void main(String[] args) {
- try {
- File file = new File("C:\\file.xml");
- JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
- Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
- Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
- System.out.println(customer);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement