-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlConnection.java
88 lines (67 loc) · 1.8 KB
/
sqlConnection.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class sqlConnection {
private final String url ="jdbc:mysql://localhost:3306/";
private String user = "root";
private String password = "13456stau";
private String db = "java";
private String command;
public static String ergStr = "";
public sqlConnection (String s) {
this.setCommand(" SELECT * FROM mainexamples WHERE name LIKE '%" +s+"%'");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(this.getUrl(), this.getUser(), this.getPassword());
Statement stt= con.createStatement();
stt.execute("USE"+" "+this.getDb());
ResultSet res = stt.executeQuery(this.getCommand());
while (res.next()) {
ergStr = ergStr.concat("\n"+res.getString("ID") + " "+ res.getString("Name")+ "\n");
}
ausgeben1();
res.close();
stt.close();
con.close();
}
catch (Exception e){
System.out.println("Error");
}
}
public static void ausgeben1() {
// TODO Auto-generated method stub
System.out.println(ergStr);
}
static void ausgeben() {
// TODO Auto-generated method stub
System.out.println(ergStr);
}
private String getCommand() {
return command;
}
private void setCommand(String command) {
this.command=command;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDb() {
return db;
}
public void setDb(String db) {
this.db = db;
}
public String getUrl() {
return url;
}
}