![](https://i-blog.csdnimg.cn/blog_migrate/bd4304e37b44ec2d5991e10decb0846e.png)
// 加载MySQL驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 建立连接
String url = "jdbc:mysql://localhost/database_name";
String username = "username";
String password = "password";
Connection conn = DriverManager.getConnection(url, username, password);
![](https://i-blog.csdnimg.cn/blog_migrate/de4e0ceaf5749dbcd2a97671666f169b.png)
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");
![](https://i-blog.csdnimg.cn/blog_migrate/aee320eb54ebf3251fae71f165232ef5.png)
String query = "SELECT * FROM table_name WHERE column = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, "value");
ResultSet rs = pstmt.execute Query();