JDBC的学习记录

MySQL :
JDBC Name: Connector/J
Download: http://dev.mysql.com/downloads/connector/j/
Conn Code:
Class.forName(“com.mysql.jdbc.Driver”);
Connection con = null!
con=DriverManager.getConnection(“jdbc:mysql://host:port/database”,”user”,”password”);

Oracle:
JDBC Name: Connector/J
Download: http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
Conn Code:
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con = null!
con=DriverManager.getConnection(“jdbc:oracle:thin:@host:port:databse”,”user”,”password”);

Class.forName()用法详解
Class.forName方法:
此方法含义是:加载参数指定的类,并且初始化它。
在使用jdbc方式连接数据库时,为什么要执行Class.forName(‘驱动类名’)方法?
因为将驱动类的class文件装载到内存中,并且形成一个描述此驱动类结构的Class类实例,并且初始化此驱动类,这样jvm就可以使用它了,这就是Class.forName()方法的含义。

MySQL的jdbc写法:

    public static void main(String[] args) throws Exception{

        Connection con = null;
        Statement stm = null;
        ResultSet res = null;

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/goods", "root", "root");

        //1、先建立媒介
        stm = con.createStatement();
        res = stm.executeQuery("select * from t_user");//executeQuery查询出来的是set集合

        //boolean flag = stm.excute("select * from t_use");//尝试excute数据库

        while(res.next()){
            System.out.println(res.getString(1));
            System.out.println(res.getString(2));
            System.out.println(res.getString(3));
            System.out.println(res.getString(4));
            System.out.println(res.getString(5));
            System.out.println(res.getString(6));
        }
        //2、直接访问
        //con.prepareStatement("select * from t_user");
        res.close();
        stm.close();
        con.close();
    }

你可能感兴趣的:(jdbc,jdbc)