Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit feb3694

Browse files
committed
Add initial project setup for TestJavaJDBC, including .gitignore, project configuration, and ConnectSQL class for database connection
1 parent f5138de commit feb3694

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.28.jar"/>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TestJavaJDBC</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
2.36 MB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package models;
2+
import java.security.PublicKey;
3+
import java.sql.*;
4+
5+
public class ConnectSQL {
6+
public static Connection getConnection() {
7+
Connection conn = null;
8+
try{
9+
String userName = "root";
10+
String password = "";
11+
String DatabaseName = "websell";
12+
String url = "jdbc:mysql://localhost/" + DatabaseName;
13+
Class.forName("com.mysql.cj.jdbc.Driver");
14+
conn = DriverManager.getConnection(url, userName, password);
15+
System.out.println("Ok");
16+
} catch (Exception e) {
17+
// TODO: handle exception
18+
e.printStackTrace();
19+
}
20+
return conn;
21+
}
22+
23+
public void showAll() {
24+
Connection conn = ConnectSQL.getConnection();
25+
26+
String query = "SELECT * FROM users";
27+
28+
PreparedStatement pstm = null;
29+
try {
30+
//Tạo đối tượng Statement
31+
pstm = conn.prepareStatement(query);
32+
33+
//gán các giá trị vào tham số
34+
// pstm.setString(1, "ngoc");
35+
36+
//Thực thi truy vấn và trả về đối tượng ResultSet
37+
ResultSet rs = pstm.executeQuery();
38+
39+
//Duyệt kết quả trả về
40+
while (rs.next()){
41+
int id = rs.getInt("id");
42+
String username = rs.getString("phoneNumber");
43+
String password = rs.getString("password");
44+
String email = rs.getString("email");
45+
int role = rs.getInt("role");
46+
47+
System.out.println(id + " - " + username + " - " + password + " - " + email + " " + role);
48+
}
49+
50+
//Đóng kết nối
51+
// conn.close();
52+
} catch (SQLException e) {
53+
e.printStackTrace();
54+
}
55+
56+
57+
58+
}
59+
60+
public void insert() {
61+
Connection conn = ConnectSQL.getConnection();
62+
63+
String query = "INSERT INTO users(id, username, password, email) " +
64+
"VALUES (null, ?,?,?)";
65+
66+
PreparedStatement pstm = null;
67+
try {
68+
pstm = conn.prepareStatement(query);
69+
70+
pstm.setString(1, "hung");
71+
pstm.setString(2, "123456789");
72+
pstm.setString(3, "hung@gmail.com");
73+
74+
//Khi thực hiện các lệnh insert/update/delete sử dụng executeUpdate(), nó sẽ trả về số hàng bị tác động
75+
int row = pstm.executeUpdate();
76+
if(row != 0){
77+
System.out.println("Thêm thành công " + row);
78+
}
79+
80+
//Đóng kết nối
81+
conn.close();
82+
} catch (SQLException e) {
83+
e.printStackTrace();
84+
}
85+
}
86+
87+
public static void main(String[] args) {
88+
ConnectSQL connectSQL = new ConnectSQL();
89+
connectSQL.showAll();
90+
}
91+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /