日期:2014-05-16  浏览次数:20425 次

jdbc相关

我很怀疑以后到公司如果不让用myeclipse,我还会不会做项目,哎,不管了,先复习一下jdbc- -

1,刚开始接触时的jdbc

刚开始接触的时候,jdbc是最简单的,没工厂,没class.forName.直接连

//加载驱动,最原始的了
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2.获得连接
String url = "jdbc:mysql://localhost:3306/etoak";
String user="root";
String pwd="etoak";
Connection con = DriverManager.getConnection(url,user,pwd);
//3.创建执行SQL的Statement对象 后来说这厮不安全 - - 让用PreparedStatement
Statement sta  = con.createStatement();
//4.执行SQL并返回结果
String sql = "select * from student";
ResultSet rs = sta.executeQuery(sql);
//5.处理结果
	while(rs.next()){
		int id = rs.getInt("id");
		String name = rs.getString("name");
		System.out.println("id===>"+id+",\t name===>"+name);
	}
//6.关闭资源
	rs.close();
	sta.close();
	con.close();

对于Statement的研究

? executeUpdate 返回值是对数据库影响的行数 不能用于select查询

execute是一个万能方法 可以执行任意的SQL语句 返回值是ture/false 由是否有ResultSet对象决定,只要有ResultSet,不管其是否是空,返回值都是true

?

?

对于ResultSet结果的处理 可以根据字段拿,也可以根据顺序拿

import java.sql.*;
//测试ResultSet 的基本用法
public class TestRS{
	public static void main(String arg[])throws Exception{
		Connection con = CF.getConnection("mysql");
		Statement sta =con.createStatement();
		String sql ="select * from student";
		ResultSet rs = sta.executeQuery(sql);
		while(rs.next()){//控制行
			int id = rs.getInt("id");//列
			String name  = rs.getString(2);
			int age = rs.getInt("age");
			Date birth = rs.getDate(4);
			System.out.println(id+"\t"+name+"\t"+age+"\t"+birth);
		}
	}
}

??

后来,说可以把获得连接那需要的参数写在一个.properties文件里.

#this is comment
#Sun Jan 06 16:55:04 CST 2013
user=root
password=etoak
url=jdbc\:mysql\:///mysql
xinbanzhang=xiaohei
driver=com.mysql.jdbc.Driver

?然后

Properties pro = new Properties();
pro.load(new FileReader(new File("etoak.properties")));

?并且加载驱动也不用最原始的方法了

//加载类com.mysql.jdbc.Driver
Class.forName(pro.getProperty("driver"));

?然后获取连接

Connection con = DriverManager.getConnection(pro.getProperty("url"),pro);

?

这个方法在api中真有
static Connection    getConnection(String url, Properties info)    试图建立到给定数据库 URL 的连接。 

?哦.忘了一个小插曲 上面提到的properties的创建,使用等

import java.util.*;
import java.io.*;

//测试Properties的具体用法 String File
public class TestPro{
	public static void main(String args[])	throws Exception{
		Properties pro = new Properties();
		Reader reader  = new FileReader("etoak.properties");
		//加载配置信息
		pro.load(reader);
		reader.close();
		//根据key获得value
		System.out.println(pro.getProperty("xinbanzhang"));
		//添加属性
		pro.setProperty("xinbanzhang","xiaohei");
		//写出 第二个是对文件的说明,注释
		pro.store(new FileOutputStream("etoak.properties"),"this is comment");
	}
}

?最后就是刚开始提到的工厂"CF"了.connection factory

这个工厂是读取本地xml文件,写到properties里.然后DriverManager获取连接

import java.sql.*;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
import java.io.*;
// 作为连接的工厂 对外提供连接
public class CF{
	//对外提供连接的方法
	public static Connection getConnection(String dbName)throws Exception{
		Properties pro = getConfig(dbName);
		String driver = pro.getProperty("driver");
		String url = pro.getProperty("url");
		String user = pro.getProperty("user");
		String pwd = pro.getProperty("password");
		Class.forName(driver);
		return DriverManager.getConnection(url,user,pwd);
	}
	private static Properties getConfig(String dbName)throws Exception{
		Properties pro = new Properties();
		//解析配置文件  返回数据库的连接信息
		SAXRe