· 8 years ago · Apr 03, 2018, 02:06 AM
1public cntctus()
2{
3
4String column[]= { "Name","Position","Phone"};
5
6Object [][]row = {
7 {"Prof. Renu Vig", "Director", "+123456"},
8 {"Mr. Sukhbir singh", "Assistant Professor", "+9123568989"},
9 {"Ms. shaweta", "BI teacher","9468645"}
10 };
11
12
13 table = new JTable(row,column);
14 TableModel tm = table.getModel();
15 java.sql.Connection con=null;
16 try {
17 Class.forName("com.mysql.jdbc.Driver");
18con=DriverManager.getConnection("jdbc:mysql://localhost:3306/training","root","");
19 try{
20 java.sql.Statement stmt =con.createStatement();
21String maketable = "CREATE TABLE if not exists contacttable(Name Varchar(25),Position Varchar(20),Phone Varchar(20))";
22 stmt.executeUpdate(maketable);
23 System.out.print("table created ");
24//insert into table contacttable query
25
26PreparedStatement pstmt=con.prepareStatement("INSERT into contacttable select distinct values(?,?,?)");
27
28for (int i = 0; i < tm.getRowCount(); i++) {
29 for (int j = 0; j < tm.getColumnCount(); j++) {
30 Object o = tm.getValueAt(i, j);
31
32
33 System.out.println("object from table is : " +o);
34 k=j+1;
35 pstmt.setString(k, (String)o);
36 }
37 pstmt.executeUpdate();
38 }
39
40 }
41 catch(SQLException s)
42 {
43 System.out.println(s);
44 }
45 }
46 catch(Exception e)
47 {
48 e.printStackTrace();
49 }
50
51String column[]= { "Name","Position","Phone"};
52 Object [][]row = {
53 {"Prof. Renu Vig", "Director", "+123456"},
54 {"Mr. Sukhbir singh", "Assistant Professor", "+9123568989"},
55 {"Ms. shaweta", "BI teacher","9468645"}
56 };
57
58 JTable table = new JTable(row,column);
59 TableModel tm = table.getModel();
60 java.sql.Connection con=null;
61 try
62 {
63
64 Class.forName("com.mysql.jdbc.Driver");
65 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/training","root","");
66
67 java.sql.Statement stmt =con.createStatement();
68 String maketable = "CREATE TABLE if not exists contacttable(Name Varchar(25),Position Varchar(20),Phone Varchar(20))";
69 stmt.executeUpdate(maketable);
70 System.out.print("table created ");
71
72 PreparedStatement pstmt=con.prepareStatement("INSERT INTO contacttable VALUES(?,?,?)");
73
74 for (int i = 0; i < tm.getRowCount(); i++) {
75 for (int j = 0; j < tm.getColumnCount(); j++) {
76 Object o = tm.getValueAt(i, j);
77 System.out.println("object from table is : " +o);
78 pstmt.setString(j+1, (String)o);
79 }
80 pstmt.executeUpdate();
81 pstmt.clearParameters();
82 }
83 }
84 catch (Exception e) {
85 e.printStackTrace();
86 }
87
88for (int i = 0; i < tm.getRowCount(); i++) {
89 for (int j = 0; j < tm.getColumnCount(); j++) {
90 Object o = tm.getValueAt(i, j);
91 System.out.println("object from table is : " +o);
92 pstmt.setObject(j+1, o);
93
94TableModel tm= table.getModel();
95
96 for (int i = 0; i < tm.getRowCount(); i++) {
97
98
99
100 Object NumeroCin=tm.getValueAt(i, 0);
101 Object Nomprenom=tm.getValueAt(i, 1);
102 Object Tel =tm.getValueAt(i, 2);
103 Object Adresse =tm.getValueAt(i, 3);
104 Object DateNaissance=tm.getValueAt(i, 5);
105 Object Sexe=tm.getValueAt(i, 4);
106
107
108
109 Etudiant e=new Etudiant();
110 e.setAdresse((String) Adresse);
111 e.setDateNaissance((String) DateNaissance);
112 e.setNomprenom((String) Nomprenom);
113 e.setNumeroCin((String) NumeroCin);
114 e.setSexe( (String) Sexe );
115 e.setTel((String) Tel);
116
117 Ajouterobjet(e);
118
119 }}}
120
121 catch (Exception e) {
122 e.printStackTrace();
123 }
124
125EntityTransaction tx=entityManager.getTransaction();
126 tx.begin();
127 entityManager.persist(o);
128 tx.commit();
129
130
131 }