public static void main(String[] args) throws ClassNotFoundException, SQLException {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/ns_cloud?useSSL=false&useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, user, password);
String preId="1";
String isBand="1";
//定义sql
String sql = "SELECT t.* FROM ns_cloud.ns_client t where id =? and is_bind=? limit 2";
//获取执行对象Statement
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"1");
preparedStatement.setString(2,isBand);
//这行sql
ResultSet resultSet = preparedStatement.executeQuery();
//处理结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("client_name");
// 其他列的数据获取
System.out.println("ID: " + id + ", Name: " + name);
}
//释放资源
preparedStatement.close();
connection.close();
}