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();

  

posted on 2021-11-19 14:52  java-go  阅读(17)  评论(0)    收藏  举报

导航