请问通过jdbc精确查询mysql数据库怎么写?
请问通过jdbc精确查询mysql数据库怎么写?
public static void main(String[] args) {
System.out.println("MySQL JDBC Example.");
Connection conn = null;
String url = "jdbc:mysql://...:3306/test?autoReconnect=true&useSSL=false";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "XXXXXXXX";
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
stmt = conn.createStatement();
String sql = "select * from AAA";
rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("emp_id");
String name = rs.getString("name");
System.out.println("id = " + id + ", name = " + name);
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
}
}
}