您的位置:首页 > 数据库

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();}}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: