所需jar包:

mysql-connector-java-5.1.27.jar

代码:

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root";
String sql1 = "INSERT...";
String sql2 = "SELECT...";

Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);

//以下两种方式均可
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql1);

PreparedStatement pstmt = conn.prepareStatement(sql2);
ResultSet rs = pstmt.executeQuery();

//最后注意关闭
rs.close();
pstmt.close();
stmt.close();
conn.close();

相关文章:

  • 2022-12-23
猜你喜欢
  • 2021-04-14
  • 2022-01-15
  • 2021-10-18
  • 2021-09-19
  • 2021-12-11
  • 2022-01-27
相关资源
相似解决方案