本文章分享JDBC链接数据的步骤,简易中的精髓。
- 链接数据的步骤
- 1.注册驱动
- driverManager.registerDriver(new com.mysql.jdbc.Driver());
-
- Class.forName("com.mysql.jdbc.Driver");//推荐
- 2.建立链接
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
- *************注解*****************
- Connection conn = DriverManager.getConnection(url,user,password)
- url格式:JDBC:子协议:之名称//主机名:端口/数据名?属性名=属性值&....
- *************************************
- 3.创建语句
- Statement st = conn.createStatement();
- 4.执行语句
- ResultSet rs = st.executeQuery("SQL语句");
- 5.处理结果
- while(rs.next()){
- .....
- }
- 6.释放资源
- rs.close();
- st.close();
- conn.close();
-