simple jdbc example to connect mysql

2020-07-28
java

java maven for jdbc

1
2
3
4
5
    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Connection conn = null;

try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/hearing?" +
"user=root&password=pass1234&serverTimezone=UTC");

String user ="test@gmail.com";
PreparedStatement st = conn.prepareStatement("SELECT * FROM Customer WHERE Email = ?");
st.setString(1,user);
ResultSet ret = st.executeQuery();
while(ret.next()) {
//here we can get the column by name
System.out.println(ret.getString("FirstName"));
System.out.println(ret.getString("Created"));
}
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}