@SuppressWarnings("all")
public class JDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建数据库连接
String url = "jdbc:mysql://localhost:3306/test";
String userName = "*******";
String password = "*******";
Connection connection = DriverManager.getConnection(url, userName, password);
//3.创建PreparedStatement执行对象,用来执行SQL语句
String sql = "select * from user";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//4.执行SQL语句
ResultSet resultSet = preparedStatement.executeQuery();
//5.遍历结果集合,处理结果
while (resultSet.next()) {
resultSet.getString(1);
}
//6.关闭数据库连接
resultSet.close();
preparedStatement.close();
connection.close();
}
}
JDBC步骤
最新推荐文章于 2023-08-17 21:15:47 发布