05-Jdbc获取数据库资源基本步骤
2016-11-10 21:45
281 查看
package cn.itcast.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.junit.Test;public class jdbcDemo3 {@Testpublic void insert() throws ClassNotFoundException{//1、加载驱动....Class.forName("com.mysql.jdbc.Driver");//2、建立连接....String
url="jdbc:mysql://localhost:3306/day07";String user="root";String password="123456";Connection conn=null;Statement stem=null;ResultSet re=null;try {conn = DriverManager.getConnection(url,user,password);//3、获取Stem对象....stem=conn.createStatement();//4、把SQL语句发送给数据库去执行....String
sql="select * from ta_user";re=stem.executeQuery(sql);//遍历SQL语句....while(re.next()){System.out.println(re.getString("uesrname"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//释放资源....try {if(re!=null){re.close();re=null;}}
catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(stem!=null){stem.close();stem=null;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(conn!=null){conn.close();conn=null;}} catch
(SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
url="jdbc:mysql://localhost:3306/day07";String user="root";String password="123456";Connection conn=null;Statement stem=null;ResultSet re=null;try {conn = DriverManager.getConnection(url,user,password);//3、获取Stem对象....stem=conn.createStatement();//4、把SQL语句发送给数据库去执行....String
sql="select * from ta_user";re=stem.executeQuery(sql);//遍历SQL语句....while(re.next()){System.out.println(re.getString("uesrname"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//释放资源....try {if(re!=null){re.close();re=null;}}
catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(stem!=null){stem.close();stem=null;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(conn!=null){conn.close();conn=null;}} catch
(SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
相关文章推荐
- 通过JDBC访问数据库的基本步骤
- 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤
- 简述JDBC调用数据库的基本步骤
- Jdbc连接数据库基本步骤
- 简述JDBC调用数据库的基本步骤
- JDBC 访问数据库的基本步骤(选出要熟记的部分
- 简述JDBC调用数据库的基本步骤
- 简述JDBC调用数据库的基本步骤
- 简述JDBC调用数据库的基本步骤
- 通过JDBC访问数据库的基本步骤
- JDBC 访问数据库的基本步骤(选出要熟记的部分)
- 简述JDBC调用数据库的基本步骤
- JDBC访问数据库的基本步骤
- 简述JDBC调用数据库的基本步骤
- JDBC 访问数据库的基本步骤(
- 项目小结-JDBC访问数据库的基本步骤
- JDBC操作数据库的基本步骤
- java中简述使用JDBC完成数据库操作的基本步骤。
- JDBC操作数据库基本步骤
- 简述JDBC调用数据库的基本步骤