读书人

jdbc相干

发布时间: 2013-01-28 11:49:56 作者: rapoo

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对象 后来说这厮不安全 - - 让用PreparedStatementStatement 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 2013user=rootpassword=etoakurl=jdbc\:mysql\:///mysqlxinbanzhang=xiaoheidriver=com.mysql.jdbc.Driver

?然后

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

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

//加载类com.mysql.jdbc.DriverClass.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 Filepublic 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获得valueSystem.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();//解析配置文件  返回数据库的连接信息SAXReader reader = new SAXReader();Document doc = reader.read(new File("db.xml"));Element root = doc.getRootElement();List<Element> firsts = root.elements("db");for(Element first:firsts){String v_id = first.attributeValue("id");//判断传入的dbName与当前读到的id的值是否一致if(dbName.equalsIgnoreCase(v_id)){pro.setProperty("driver",first.elementTextTrim("driver"));pro.setProperty("url",first.elementTextTrim("url"));pro.setProperty("user",first.elementTextTrim("user"));pro.setProperty("password",first.elementTextTrim("pwd"));}}return pro;}}

?

?

?

读书人网 >其他数据库

热点推荐