jdbc六步

JDBC六步

1.注册驱动
2.创建连接
3.获取操作sql语句对象
4.执行sql语句(并接收结果集)
(5.处理结果集)
6.关闭资源,(先开后关),

`import java.sql.*;

public class JdbcDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建数据库连接对象
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "1234");
//3.创建执行sql语句对象
Statement stt = con.createStatement();
//4.执行sql语句,(并接收结果集)
String sql = "select * from jdbcdemo";
ResultSet rs = stt.executeQuery(sql);
//5.处理结果集
while (rs.next()) {
System.out.println(rs.getString("id") + rs.getString("username") + rs.getString("password"));
}
//6.关闭资源,(先开后关),
rs.close();
stt.close();
con.close();
}

}`

代码执行结果:

posted @ 2021-06-10 16:54  一只带不动的小菜鸡  阅读(45)  评论(0编辑  收藏  举报