-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMySqlConnector.java
More file actions
46 lines (41 loc) · 1.24 KB
/
MySqlConnector.java
File metadata and controls
46 lines (41 loc) · 1.24 KB
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
package DBClients;
import java.sql.*;
/**
* Created by deepak.jayaprakash on 31/10/18.
*/
public class MySqlConnector {
// mysql -h localhost -u root
public static Connection getConnection() {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "");
return con;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void executeMethod() {
// Connection connection = getConnection(); // from native jdbc
Connection connection = MySqlHikariSource.getInstance().getConnection(); // using hikari
if (connection == null)
return;
ResultSet rs;
try {
Statement stmt = connection.createStatement();
rs = stmt.executeQuery("select count(*) from users");
while (rs.next())
System.out.println(rs.getInt(1));
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
executeMethod();
}
}