-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaConnection.java
191 lines (158 loc) · 5.5 KB
/
JavaConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.*;
import java.util.Vector;
import javax.swing.table.*;
import javax.swing.*;
/**
*
* @author user
*/
public class JavaConnection {
Connection con = null;
Statement st = null;
ResultSet rs;
void connection() {
String dbname = "db1";
try {
JavaConnection db = new JavaConnection();
db.connectionInitial();
ResultSet rs1 = db.executeQuery("Select name from currentdb where id=1");
if (rs1.next()) {
dbname = rs1.getString(1);
}
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbname , "admin", "12345");
// It creates and displays the table
// Closes the Connection
// The Connection is obtained
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "please Connect Xamp Start Mysql","Error the Developer Wants You to Start Xamp/Wamp",JOptionPane.ERROR_MESSAGE);
}
}
void connectionInitial() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
// It creates and displays the table
// Closes the Connection
// The Connection is obtained
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "please Connect Xamp Start Mysql","Error the Developer Wants You to Start Xamp/Wamp",JOptionPane.ERROR_MESSAGE);
}
}
int executeUpdate(String s) {
int r = 0;
try {
st = con.createStatement();
r = st.executeUpdate(s);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
return r;
}
void execute(String s) {
try {
st = con.createStatement();
st.execute(s);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
ResultSet executeQuery(String s) {
try {
st = con.createStatement();
rs = st.executeQuery(s);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
return rs;
}
/* void empDb() {
JavaConnection db = new JavaConnection();
db.connection();
try {
ResultSet rs;
rs = db.Execute("select id,name,age from emp");
if (rs != null) {
while (rs.next()) {
System.out.println(r.getInt(1) + " " + r.getString(2) + " " + r.getString(3));
}
} else {
System.out.println("hey here is null");
}
} catch (Exception e) {
System.out.println(e);
}
db.connClose();
}
*/
void connClose() {
try {
this.con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
void empTable() {
try {
JavaConnection db = new JavaConnection();
db.connection();
ResultSet rs;
rs = db.executeQuery("select * from addition");
TableModelJTable t = new TableModelJTable();
JFrame f = new JFrame();
JTable table = new JTable(t.buildTableModel(rs));
JScrollPane sp = new JScrollPane(table);
f.add(sp);
f.setSize(300, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
class TableModelJTable {
static DefaultTableModel db;
// it is not predifines function
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
try {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
db = new DefaultTableModel(data, columnNames);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
return db;
}
}
class ab {
public static void main(String[] args) {
JavaConnection db = new JavaConnection();
db.empTable();
// db.empDb();
// db.connClose();
}
}