Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.74 KB | None | 0 0
  1. package application;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Date;
  5.  
  6. import javax.xml.bind.annotation.XmlAttribute;
  7. import javax.xml.bind.annotation.XmlElement;
  8. import javax.xml.bind.annotation.XmlRootElement;
  9.  
  10. @XmlRootElement
  11. public class Autoturism implements Serializable{
  12.  
  13. @Override
  14. public String toString() {
  15. return "Autoturism [nrMasina=" + nrMasina + ", idPermisAuto=" + idPermisAuto + ", pretPerZi=" + pretPerZi
  16. + ", deCand=" + deCand + ", panaCand=" + panaCand + "]";
  17. }
  18. private String nrMasina;
  19. private String idPermisAuto;
  20. private float pretPerZi;
  21. private Date deCand;
  22. private Date panaCand;
  23.  
  24. public String getNrMasina() {
  25. return nrMasina;
  26. }
  27. @XmlAttribute
  28. public void setNrMasina(String nrMasina) {
  29. this.nrMasina = nrMasina;
  30. }
  31. public String getIdPermisAuto() {
  32. return idPermisAuto;
  33. }
  34. @XmlElement
  35. public void setIdPermisAuto(String idPermisAuto) {
  36. if(idPermisAuto !=null)
  37. {
  38. this.idPermisAuto = idPermisAuto;
  39. }
  40. else
  41. idPermisAuto="NIMIC";
  42. }
  43. public float getPretPerZi() {
  44. return pretPerZi;
  45. }
  46. @XmlElement
  47. public void setPretPerZi(float pretPerZi) {
  48. this.pretPerZi = pretPerZi;
  49. }
  50. public Date getDeCand() {
  51. return deCand;
  52. }
  53. @XmlElement
  54. public void setDeCand(Date deCand) {
  55. if(deCand!=null)
  56. {
  57. this.deCand = deCand;
  58. }
  59. }
  60. public Date getPanaCand() {
  61. return panaCand;
  62. }
  63. @XmlElement
  64. public void setPanaCand(Date panaCand) {
  65. if(panaCand!=null)
  66. {
  67. this.panaCand = panaCand;
  68. }
  69. }
  70. public Autoturism(String nrMasina, String idPermisAuto, float pretPerZi, Date deCand, Date panaCand) {
  71. super();
  72. this.nrMasina = nrMasina;
  73. this.idPermisAuto = idPermisAuto;
  74. this.pretPerZi = pretPerZi;
  75. this.deCand = deCand;
  76. this.panaCand = panaCand;
  77. }
  78. public Autoturism() {
  79. super();
  80. }
  81.  
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. package application;
  92.  
  93. import java.util.Date;
  94.  
  95. public class Inchiriere {
  96.  
  97. private String idPermisAuto;
  98. private Date deCand;
  99. public Inchiriere(String idPermisAuto, Date deCand, Date panaCand) {
  100. super();
  101. this.idPermisAuto = idPermisAuto;
  102. this.deCand = deCand;
  103. this.panaCand = panaCand;
  104. }
  105.  
  106. public String getIdPermisAuto() {
  107. return idPermisAuto;
  108. }
  109.  
  110. public void setIdPermisAuto(String idPermisAuto) {
  111. this.idPermisAuto = idPermisAuto;
  112. }
  113.  
  114. public Date getDeCand() {
  115. return deCand;
  116. }
  117.  
  118. public void setDeCand(Date deCand) {
  119. this.deCand = deCand;
  120. }
  121.  
  122. public Date getPanaCand() {
  123. return panaCand;
  124. }
  125.  
  126. public void setPanaCand(Date panaCand) {
  127. this.panaCand = panaCand;
  128. }
  129.  
  130. private Date panaCand;
  131.  
  132. public float getPretInchiriere(Autoturism a)
  133. {
  134. return a.getPretPerZi();
  135. }
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. package application;
  146.  
  147. import java.io.IOException;
  148. import java.io.ObjectInputStream;
  149. import java.net.Socket;
  150. import java.net.UnknownHostException;
  151. import java.util.LinkedList;
  152. import java.util.List;
  153.  
  154. public class Client implements Runnable{
  155.  
  156. public static List<Autoturism>autoturisme=new LinkedList<Autoturism>();
  157. public static List<Inchiriere>inchiriate= new LinkedList<Inchiriere>();
  158.  
  159. private Socket socket=null;
  160. private ObjectInputStream in=null;
  161.  
  162. @Override
  163. public void run() {
  164.  
  165. try {
  166. socket= new Socket("localhost",7000);
  167. in=new ObjectInputStream(socket.getInputStream());
  168. System.out.println("s-a conectat");
  169. while(true)
  170. {
  171. autoturisme=(List<Autoturism>) in.readObject();
  172. for(Autoturism a : autoturisme)
  173. {
  174. Inchiriere inch= new Inchiriere(a.getIdPermisAuto(),a.getDeCand(),a.getPanaCand());
  175. inchiriate.add(inch);
  176. System.out.println(a);
  177. }
  178. }
  179. } catch (UnknownHostException e) {
  180. // TODO Auto-generated catch block
  181. e.printStackTrace();
  182. } catch (IOException e) {
  183. // TODO Auto-generated catch block
  184. e.printStackTrace();
  185. } catch (ClassNotFoundException e) {
  186. // TODO Auto-generated catch block
  187. e.printStackTrace();
  188. }
  189. // finally {
  190. // try {
  191. // in.close();
  192. // socket.close();
  193. // } catch (IOException e) {
  194. // // TODO Auto-generated catch block
  195. // e.printStackTrace();
  196. // }
  197. // }
  198. }
  199.  
  200. public Client(Socket socket, ObjectInputStream in) {
  201. super();
  202. this.socket = socket;
  203. this.in = in;
  204. }
  205.  
  206. public Client() {
  207. // TODO Auto-generated constructor stub
  208. }
  209.  
  210. public static void main(String[] args) {
  211.  
  212. Client c= new Client();
  213. Thread t = new Thread(c);
  214. t.start();
  215. }
  216.  
  217.  
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. package application;
  226.  
  227. import java.io.BufferedReader;
  228. import java.io.File;
  229. import java.io.FileNotFoundException;
  230. import java.io.FileReader;
  231. import java.io.IOException;
  232. import java.io.ObjectOutputStream;
  233. import java.net.ServerSocket;
  234. import java.net.Socket;
  235. import java.sql.Connection;
  236. import java.sql.DriverManager;
  237. import java.sql.PreparedStatement;
  238. import java.sql.ResultSet;
  239. import java.sql.SQLException;
  240. import java.sql.Statement;
  241. import java.text.ParseException;
  242. import java.text.SimpleDateFormat;
  243. import java.util.ArrayList;
  244. import java.util.Date;
  245. import java.util.LinkedList;
  246. import java.util.List;
  247.  
  248. import javax.xml.bind.JAXBContext;
  249. import javax.xml.bind.JAXBException;
  250. import javax.xml.bind.Marshaller;
  251. import javax.xml.bind.annotation.XmlAccessorType;
  252.  
  253.  
  254. public class Server implements Runnable{
  255.  
  256. public static List<Autoturism>auto= new ArrayList<Autoturism>();
  257.  
  258. private ServerSocket server= null;
  259. private Socket socket=null;
  260. private ObjectOutputStream out=null;
  261.  
  262. public Server() {
  263. super();
  264. Citeste();
  265. //bazaDate();
  266. mozilla();
  267. Xml();
  268. }
  269.  
  270. public void mozilla()
  271. {
  272. try {
  273. Class.forName("org.sqlite.JDBC");
  274. Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\mihai1\\Documents\\Eclipse\\Auto.sqlite");
  275. Statement st =conn.createStatement();
  276. //st.execute("create table Auto(nrMasina string,idPermisAuto string,pretPerZi float,deCand string,panaCand string)");
  277. for(Autoturism a : auto)
  278. {
  279. st.execute("insert into Auto(nrMasini,idPermisAuto,pretPerZi,deCand,panaCand)"+"values('"+a.getNrMasina()+"','"+a.getIdPermisAuto()+"',"+a.getPretPerZi()+",'"+a.getDeCand().toString()+"','"+a.getPanaCand().toString()+"')");
  280. }
  281. st.close();
  282. conn.close();
  283. } catch (ClassNotFoundException e) {
  284. // TODO Auto-generated catch block
  285. e.printStackTrace();
  286. } catch (SQLException e) {
  287. // TODO Auto-generated catch block
  288. e.printStackTrace();
  289. }
  290. }
  291.  
  292. public void bazaDate()
  293. {
  294. try {
  295. Class.forName("org.sqlite.JDBC");
  296.  
  297. Connection con= DriverManager.getConnection("jdbc:sqlite:Masini.db");
  298. Statement st =con.createStatement();
  299. st.execute("create table Masini(nrMasina string,idPermisAuto string,pretPerZi float,deCand string,panaCand string)");
  300. for(Autoturism a : auto)
  301. {
  302. st.execute("insert into Masini(nrMasina,idPermisAuto,pretPerZi,deCand,panaCand)"+"values('"+a.getNrMasina()+"','"+a.getIdPermisAuto()+"',"+a.getPretPerZi()+",'"+a.getDeCand().toString()+"','"+a.getPanaCand().toString()+"')");
  303. }
  304. String comanda="select * from Masini";
  305. ResultSet rs=st.executeQuery(comanda);
  306. while(rs.next())
  307. {
  308. String nr=rs.getString("nrMasina");
  309. String id=rs.getString("idPermisAuto");
  310. float pret=rs.getInt("pretPerZi");
  311. String cand=rs.getString("deCand");
  312. String pana=rs.getString("panaCand");
  313. System.out.println(nr);
  314. System.out.println(id);
  315. System.out.println(pret);
  316. System.out.println(cand);
  317. System.out.println(pana);
  318. }
  319. rs.close();
  320. con.close();
  321. } catch (ClassNotFoundException e) {
  322. // TODO Auto-generated catch block
  323. e.printStackTrace();
  324. } catch (SQLException e) {
  325. // TODO Auto-generated catch block
  326. e.printStackTrace();
  327. }
  328.  
  329. }
  330.  
  331. public void Citeste()
  332. {
  333. File f = new File("Auto.txt");
  334. try {
  335. FileReader fr = new FileReader(f);
  336. BufferedReader br = new BufferedReader(fr);
  337. String linie=br.readLine();
  338. while(linie!=null)
  339. {
  340. String[] ex=linie.split(",");
  341. String numar=ex[0];
  342. String id=ex[1];
  343. float pret=Float.parseFloat(ex[2]);
  344. SimpleDateFormat data= new SimpleDateFormat("dd/mm/yyyy");
  345. Date de=data.parse(ex[3]);
  346. Date pana=data.parse(ex[4]);
  347. Autoturism at= new Autoturism(numar,id,pret,de,pana);
  348. auto.add(at);
  349. linie=br.readLine();
  350. }
  351. } catch (FileNotFoundException e) {
  352. // TODO Auto-generated catch block
  353. e.printStackTrace();
  354. } catch (IOException e) {
  355. // TODO Auto-generated catch block
  356. e.printStackTrace();
  357. } catch (ParseException e) {
  358. // TODO Auto-generated catch block
  359. e.printStackTrace();
  360. }
  361. }
  362.  
  363. public void Xml()
  364. {
  365. File f = new File("Xml.xml");
  366. try {
  367.  
  368. JAXBContext con=JAXBContext.newInstance(ListaAuto.class);
  369. Marshaller mar=con.createMarshaller();
  370. mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  371. ListaAuto q= new ListaAuto();
  372. q.setLst(auto);
  373. mar.marshal(q, f);
  374. //mar.marshal(auto, System.out);
  375. } catch (JAXBException e) {
  376. // TODO Auto-generated catch block
  377. e.printStackTrace();
  378. }
  379. }
  380.  
  381. @Override
  382. public void run() {
  383.  
  384. try {
  385. server=new ServerSocket(7000);
  386. socket=server.accept();
  387. out= new ObjectOutputStream(socket.getOutputStream());
  388. System.out.println("merge");
  389.  
  390.  
  391. out.writeObject(auto);
  392.  
  393. while(true)
  394. {
  395.  
  396. }
  397.  
  398. } catch (IOException e) {
  399. // TODO Auto-generated catch block
  400. e.printStackTrace();
  401. }
  402. // finally {
  403. // try {
  404. // out.close();
  405. // socket.close();
  406. // } catch (IOException e) {
  407. // // TODO Auto-generated catch block
  408. // e.printStackTrace();
  409. // }
  410. //
  411. // }
  412. }
  413.  
  414.  
  415.  
  416. public static void main(String[] args) {
  417.  
  418. Server ser= new Server();
  419. Thread tt = new Thread(ser);
  420. tt.start();
  421. }
  422.  
  423.  
  424.  
  425. }
  426.  
  427.  
  428.  
  429.  
  430. package application;
  431.  
  432. import java.util.List;
  433.  
  434. import javax.xml.bind.annotation.XmlAccessorType;
  435. import javax.xml.bind.annotation.XmlAttribute;
  436. import javax.xml.bind.annotation.XmlElement;
  437. import javax.xml.bind.annotation.XmlRootElement;
  438.  
  439. @XmlRootElement
  440. public class ListaAuto {
  441.  
  442. private List<Autoturism> lst= null;
  443.  
  444. public List<Autoturism> getLst() {
  445. return lst;
  446. }
  447. @XmlElement
  448. public void setLst(List<Autoturism> lst) {
  449. this.lst = lst;
  450. }
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458. }
  459.  
  460.  
  461.  
  462. -----------------------------------------------------------------------
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. package application;
  470.  
  471. import java.io.Serializable;
  472.  
  473. public class Student implements Serializable{
  474.  
  475. private int varsta;
  476. private String nume;
  477.  
  478. public int getVarsta() {
  479. return varsta;
  480. }
  481. public void setVarsta(int varsta) {
  482. this.varsta = varsta;
  483. }
  484. public String getNume() {
  485. return nume;
  486. }
  487. public void setNume(String nume) {
  488. this.nume = nume;
  489. }
  490.  
  491. public Student(int varsta, String nume) {
  492. super();
  493. this.varsta = varsta;
  494. this.nume = nume;
  495. }
  496. @Override
  497. public String toString() {
  498. return "Student [varsta=" + varsta + ", nume=" + nume + "]";
  499. }
  500. public Student() {
  501. super();
  502. }
  503.  
  504.  
  505.  
  506. }
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516. package application;
  517.  
  518. import java.io.BufferedReader;
  519. import java.io.File;
  520. import java.io.FileReader;
  521. import java.io.IOException;
  522. import java.io.ObjectOutputStream;
  523. import java.net.Socket;
  524. import java.util.LinkedList;
  525. import java.util.List;
  526.  
  527. public class Client {
  528. public static List<Student>lista = new LinkedList<Student>();
  529. private Socket s=null;
  530. private ObjectOutputStream out=null;
  531. private boolean conectat=false;
  532.  
  533. public Client() {
  534. super();
  535. }
  536.  
  537. public void comunicare()
  538. {
  539. while(!conectat)
  540. {
  541. try {
  542. s= new Socket("localhost",7001);
  543. System.out.println("conectat");
  544. conectat=true;
  545. File f = new File("Studenti.txt");
  546. FileReader fr= new FileReader(f);
  547. BufferedReader br = new BufferedReader(fr);
  548. String line=br.readLine();
  549. while(line!=null)
  550. {
  551. int id=Integer.parseInt(line);
  552. line=br.readLine();
  553. String n=line;
  554. line=br.readLine();
  555. Student stud= new Student(id,n);
  556. lista.add(stud);
  557. }
  558. out=new ObjectOutputStream(s.getOutputStream());
  559. //Student student= new Student(21,"Puia");
  560. out.writeObject(lista);
  561. out.close();
  562. s.close();
  563. } catch (IOException e) {
  564. // TODO Auto-generated catch block
  565. e.printStackTrace();
  566. }
  567.  
  568. }
  569. }
  570.  
  571. public static void main(String[] args) {
  572.  
  573. Client c= new Client();
  574. c.comunicare();
  575.  
  576. }
  577.  
  578.  
  579.  
  580. }
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594. package application;
  595.  
  596. import java.io.BufferedWriter;
  597. import java.io.File;
  598. import java.io.FileInputStream;
  599. import java.io.FileOutputStream;
  600. import java.io.FileWriter;
  601. import java.io.IOException;
  602. import java.io.ObjectInputStream;
  603. import java.io.ObjectOutputStream;
  604. import java.net.ServerSocket;
  605. import java.net.Socket;
  606. import java.util.LinkedList;
  607. import java.util.List;
  608.  
  609. import jdk.jfr.events.FileWriteEvent;
  610.  
  611. public class Server {
  612.  
  613. public static List<Student>lista= new LinkedList<Student>();
  614. public static List<Student>studenti= new LinkedList<Student>();
  615. private ServerSocket ss=null;
  616. private Socket socket=null;
  617. private ObjectInputStream in=null;
  618.  
  619.  
  620. public Server() {
  621. super();
  622. }
  623.  
  624.  
  625. public void comunicare()
  626. {
  627. try {
  628. ss=new ServerSocket(7001);
  629. socket=ss.accept();
  630. in=new ObjectInputStream(socket.getInputStream());
  631. //Student student= (Student) in.readObject();
  632. lista=(List<Student>) in.readObject();
  633. for(Student q : lista)
  634. {
  635. System.out.println("obiectul trimis: "+ q);
  636. }
  637. File f = new File("fis.txt");
  638. FileWriter fw = new FileWriter(f);
  639. BufferedWriter bw = new BufferedWriter(fw);
  640. for(Student sss : lista)
  641. {
  642. bw.write(sss.toString());
  643. bw.newLine();
  644. }
  645.  
  646. FileOutputStream fos = new FileOutputStream("binar.dat");
  647. ObjectOutputStream oos = new ObjectOutputStream(fos);
  648. oos.writeObject(lista);
  649.  
  650. System.out.println("-----------");
  651.  
  652. FileInputStream fis= new FileInputStream("binar.dat");
  653. ObjectInputStream ois= new ObjectInputStream(fis);
  654. studenti=(List<Student>) ois.readObject();
  655. System.out.println(studenti);
  656. bw.close();
  657. fw.close();
  658. socket.close();
  659. } catch (IOException e) {
  660. // TODO Auto-generated catch block
  661. e.printStackTrace();
  662. } catch (ClassNotFoundException e) {
  663. // TODO Auto-generated catch block
  664. e.printStackTrace();
  665. }
  666.  
  667. }
  668.  
  669. public static void main(String[] args) {
  670. Server server=new Server();
  671. server.comunicare();
  672.  
  673. }
  674.  
  675. }
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682. ------------------------------------------------------------------------
  683.  
  684.  
  685.  
  686.  
  687. package application;
  688.  
  689. import java.io.BufferedReader;
  690. import java.io.BufferedWriter;
  691. import java.io.File;
  692. import java.io.FileNotFoundException;
  693. import java.io.FileReader;
  694. import java.io.FileWriter;
  695. import java.io.IOException;
  696. import java.io.ObjectOutputStream;
  697. import java.net.Socket;
  698. import java.net.UnknownHostException;
  699. import java.util.LinkedList;
  700. import java.util.List;
  701.  
  702. import javax.xml.bind.ParseConversionEvent;
  703.  
  704. import jdk.jfr.events.FileWriteEvent;
  705.  
  706. public class Client implements Runnable{
  707.  
  708. private Socket socket=null;
  709. private ObjectOutputStream out=null;
  710. public static List<Persoana>persoane=new LinkedList<Persoana>();
  711.  
  712. public Client(Socket socket, ObjectOutputStream out) {
  713. super();
  714. this.socket = socket;
  715. this.out = out;
  716. }
  717.  
  718.  
  719. public void citire()
  720. {
  721. File f = new File("fis.txt");
  722. try {
  723. FileReader fr = new FileReader(f);
  724. BufferedReader br = new BufferedReader(fr);
  725. String line=br.readLine();
  726. while(line!=null)
  727. {
  728. String[] ex=line.split(",");
  729. int v=Integer.parseInt(ex[0]);
  730. String n=ex[1];
  731. Persoana p = new Persoana(v,n);
  732. persoane.add(p);
  733. //System.out.println(p);
  734. line=br.readLine();
  735. }
  736. br.close();
  737. fr.close();
  738. } catch (FileNotFoundException e) {
  739. // TODO Auto-generated catch block
  740. e.printStackTrace();
  741. } catch (IOException e) {
  742. // TODO Auto-generated catch block
  743. e.printStackTrace();
  744. }
  745. }
  746.  
  747.  
  748.  
  749. public Client() {
  750. super();
  751. citire();
  752.  
  753. }
  754.  
  755.  
  756. @Override
  757. public void run() {
  758. try {
  759. socket=new Socket("localhost",5000);
  760. out=new ObjectOutputStream(socket.getOutputStream());
  761.  
  762.  
  763.  
  764. out.writeObject(persoane);
  765.  
  766.  
  767. } catch (UnknownHostException e) {
  768. // TODO Auto-generated catch block
  769. e.printStackTrace();
  770. } catch (IOException e) {
  771. // TODO Auto-generated catch block
  772. e.printStackTrace();
  773. }
  774.  
  775. while(true)
  776. {
  777.  
  778. }
  779.  
  780. }
  781.  
  782. public static void main(String[] args) {
  783. Client c= new Client();
  784. Thread t1 = new Thread(c);
  785. t1.start();
  786.  
  787. }
  788.  
  789.  
  790.  
  791.  
  792.  
  793. }
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803. package application;
  804.  
  805. import java.io.File;
  806. import java.io.IOException;
  807. import java.io.ObjectInputStream;
  808. import java.net.ServerSocket;
  809. import java.net.Socket;
  810. import java.util.LinkedList;
  811. import java.util.List;
  812.  
  813. import javax.xml.bind.JAXBContext;
  814. import javax.xml.bind.JAXBException;
  815. import javax.xml.bind.Marshaller;
  816.  
  817.  
  818.  
  819. public class Server implements Runnable{
  820.  
  821. public static List<Persoana>pers= new LinkedList<Persoana>();
  822. public static List<Persoana>ppp=new LinkedList<Persoana>();
  823.  
  824. private ServerSocket server=null;
  825. private Socket socket=null;
  826. private ObjectInputStream in=null;
  827.  
  828.  
  829.  
  830. public Server() {
  831. super();
  832. Mars();
  833. }
  834.  
  835.  
  836. public Server(ServerSocket server, Socket socket, ObjectInputStream in) {
  837. super();
  838. this.server = server;
  839. this.socket = socket;
  840. this.in = in;
  841. }
  842.  
  843. public void Mars()
  844. {
  845. File f = new File("marmar.xml");
  846. try {
  847. JAXBContext cont =JAXBContext.newInstance(ListaPersoane.class);
  848. Marshaller mar=cont.createMarshaller();
  849. mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  850. ListaPersoane q=new ListaPersoane();
  851. q.setListaPers(ppp);
  852. mar.marshal(q, f);
  853. } catch (JAXBException e) {
  854. // TODO Auto-generated catch block
  855. e.printStackTrace();
  856. }
  857.  
  858. }
  859.  
  860.  
  861. @Override
  862. public void run() {
  863. try {
  864. server=new ServerSocket(5000);
  865. socket=server.accept();
  866. in=new ObjectInputStream(socket.getInputStream());
  867. while(true)
  868. {
  869. pers=(List<Persoana>) in.readObject();
  870. for(Persoana p : pers)
  871. {
  872. System.out.println("Server: "+p);
  873. Persoana o= new Persoana(p.getVarsta(),p.getNume());
  874. ppp.add(o);
  875. }
  876. }
  877. } catch (IOException e) {
  878. // TODO Auto-generated catch block
  879. e.printStackTrace();
  880. } catch (ClassNotFoundException e) {
  881. // TODO Auto-generated catch block
  882. e.printStackTrace();
  883. }
  884.  
  885.  
  886.  
  887.  
  888. }
  889.  
  890.  
  891. @Override
  892. public String toString() {
  893. return "Server [server=" + server + ", socket=" + socket + ", in=" + in + "]";
  894. }
  895.  
  896.  
  897. public static void main(String[] args) {
  898. Server s= new Server();
  899. Thread t2 = new Thread(s);
  900. t2.start();
  901.  
  902. }
  903.  
  904.  
  905.  
  906. }
  907.  
  908.  
  909.  
  910.  
  911.  
  912. --------------------------------------------------------------------------
  913.  
  914.  
  915.  
  916.  
  917. package main;
  918.  
  919. import java.sql.Connection;
  920. import java.sql.PreparedStatement;
  921. import java.sql.ResultSet;
  922. import java.sql.SQLException;
  923.  
  924. import org.sqlite.SQLiteConnection;
  925.  
  926. import com.sun.java_cup.internal.runtime.Symbol;
  927.  
  928. public class Main extends Application{
  929.  
  930. public static void main(String[] args) {
  931.  
  932.  
  933. String querry = "select * from caini";
  934.  
  935. Connection conn = clase.SQLiteConnection.conectare();
  936. try {
  937. PreparedStatement ps = conn.prepareStatement(querry);
  938. ResultSet rs=ps.executeQuery();
  939. while(rs.next()) //cat timp avem un urmator
  940. {
  941. System.out.println(rs.getString("nume")+" este un "+rs.getString("rasa"));
  942. }
  943. } catch (SQLException e) {
  944. // TODO Auto-generated catch block
  945. e.printStackTrace();
  946. }
  947. try
  948. {
  949. conn.close();
  950. }
  951. catch(Exception ex)
  952. {
  953. System.out.println("eroare");
  954. }
  955. }
  956.  
  957. }
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967. package clase;
  968.  
  969. import java.sql.Connection;
  970. import java.sql.DriverManager;
  971. import java.sql.SQLException;
  972.  
  973.  
  974.  
  975. public class SQLiteConnection {
  976.  
  977. Connection conn;
  978.  
  979. public static Connection conectare()
  980. {
  981. try {
  982. Class.forName("org.sqlite.JDBC"); //ii specificam JDBC-ul
  983. Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\mihai1\\Documents\\Student.sqlite"); //se realizeaza conexiunea
  984. System.out.println("Ne-am conectat");
  985. return conn; //se returneaza conexiunea
  986. } catch (ClassNotFoundException e) {
  987. // TODO Auto-generated catch block
  988. e.printStackTrace();
  989. } catch (SQLException e) {
  990. // TODO Auto-generated catch block
  991. e.printStackTrace();
  992. }
  993. return null; // daca imi intra pe catch sa-mi dea null
  994. }
  995. }
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004. ----------------------------------------------------------
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012. public class JAXBExample {
  1013. public static void main(String[] args) {
  1014.  
  1015. try {
  1016.  
  1017. File file = new File("C:\\file.xml");
  1018. JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  1019.  
  1020. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  1021. Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
  1022. System.out.println(customer);
  1023.  
  1024. } catch (JAXBException e) {
  1025. e.printStackTrace();
  1026. }
  1027.  
  1028. }
  1029. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement