jdbc连接数据库
jdbc六大步骤:
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
// 创建一个preparedStatement , 赋值占位符
PreparedStatement ps = connection.prepareStatement("select * from user_info where id = ?");
ps.setString(1, "1");
// 执行sql
ResultSet result = ps.executeQuery();
// 获取数据
while(result.next()) {
System.out.println(result.getString(1) + "--" + result.getString(2));
}
// 关闭链接
result.close();
ps.close();
connection.close();
浙公网安备 33010602011771号