Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之02.JDBC Connection

  1. 云栖社区>
  2. 博客>
  3. 正文

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之02.JDBC Connection

技术小美 2017-11-22 18:42:00 浏览872
展开阅读全文
本季我们来学习下JDBC Connection连接、获得数据连接Connection及获得一个可配置的连接
Connection简介
(1)获得数据库连接是操作数据库的第一步,是应用程序和数据库的一个“握手”过程 
(2)数据库打开的连接数是有限的,所以连接使用完成需要关闭
获得数据连接Connection
–获得数据库连接的步骤 
• Class.forName加载驱动 
Class.forName("com.mysql.jdbc.Driver"); 
• DriverManager获得连接 
DriverManager是获得数据连接的一个工厂(实例化的作用) 
» DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db", "root", "mysqladmin");
Class.forName("com.mysql.jdbc.Driver");    
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/michael_db", "root", "mysqladmin");
ConnectionUtil.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DriverManager;    

public class ConnectionUtil {    

        /**    
         * @param args    
         */
    
        public Connection getConnection(){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName("com.mysql.jdbc.Driver");    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    


image
Main.java
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    

public class Main {    

        /**    
         * @param args    
         */
    
        public static void main(String[] args) {    
                ConnectionUtil cu = new ConnectionUtil();    
                System.out.print(cu.getConnection());    
        }    


image
测试结果:
image
下面我们来看下第二种写法哈~
ConnectionUtil.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DriverManager;    

public class ConnectionUtil {    
        //第一种方法    
        public Connection getConnection(){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName("com.mysql.jdbc.Driver");    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第二种方法    
        public Connection getConnection(String driver,String url,String user,String password){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    


image
Main.java
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    

public class Main {    

        /**    
         * @param args    
         */
    
        public static void main(String[] args) {    
                ConnectionUtil cu = new ConnectionUtil();    
                //第一种方法    
                System.out.println(cu.getConnection());    
                //第二种方法    
                System.out.println(cu.getConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
        }    


image
测试结果:
image
获得一个可配置的连接
数据库的连接参数会经常改变哈~我们可以将连接参数写在配置文件当中,这也是第三种获取数据连接的方法,可以不修改源程序哈~
DBConfig.properties
driver=com.mysql.jdbc.Driver    
url=jdbc:mysql://localhost:3306/jdbc_db    
user=root    
password=mysqladmin
  
ConnectionUtil.java 
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DriverManager;    
import java.util.Properties;    

public class ConnectionUtil {    
        //第一种方法    
        public Connection getConnection(){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName("com.mysql.jdbc.Driver");    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第二种方法    
        public Connection getConnection(String driver,String url,String user,String password){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第三种方法    
        public Connection openConnection(){    
                String driver = "";    
                String url = "";    
                String user = "";    
                String password = "";    
                Properties prop = new Properties();    
                Connection conn = null;    
                try {    
                        //加载属性文件    
                        prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));    
                        driver = prop.getProperty("driver");    
                        url = prop.getProperty("url");    
                        user = prop.getProperty("user");    
                        password = prop.getProperty("password");    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    


image
Main.java
package com.michael.main;    

import com.michael.jdbc.ConnectionUtil;    

public class Main {    

        /**    
         * @param args    
         */
    
        public static void main(String[] args) {    
                ConnectionUtil cu = new ConnectionUtil();    
                //第一种方法    
                System.out.println("第一种方法:"+cu.getConnection());    
                //第二种方法    
                System.out.println("第二种方法:"+cu.getConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin"));    
                //第三种方法    
                System.out.println("第三种方法:"+cu.openConnection());    
        }    


image
测试结果:
image
#################Michael分割线#########################
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发技术讨论技术圈:[url]http://g.51cto.com/itedu[/url]
#################Michael分割线#########################








本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/151093,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
技术小美
+ 关注