连接数据库 取得 Connection try i string url dbc: mysql://localhost: 3306/demo? " nuser=root &password=123 Connection conn DriverManager getConnection(url)i catch(SQLException e) String url ="jdbc: mysql: //localhost: 3306/demo"; String user =rooti String password =123 Connection conn DriverManager getConnection(url, user, password)i
连接数据库 • 取得Connection try { String url = "jdbc:mysql://localhost:3306/demo?" + "user=root&password=123"; Connection conn = DriverManager.getConnection(url); .... } catch(SQLException e) { .... } String url = "jdbc:mysql://localhost:3306/demo"; String user = "root"; String password = "123"; Connection conn = DriverManager.getConnection(url, user, password);
简单的 Connection工具类另 取得 Connection的方式,依所使用的环境 及程序需求而有所不同 设计一个 DBSource界面 package onlyfun caterpillar import java. sql Connection; import java. sql. SQLExceptioni public interface DB Source public Connection getConnection() throws SQLException; public void closeConnection(Connection conn) throws SQLException
简单的Connection工具类别 • 取得Connection的方式,依所使用的环境 及程序需求而有所不同 • 设计一个DBSource界面 package onlyfun.caterpillar; import java.sql.Connection; import java.sql.SQLException; public interface DBSource { public Connection getConnection() throws SQLException; public void closeConnection(Connection conn) throws SQLException; }
简单的 Connection工具类别 public class simple DB SourceimplementsDBSource public simpleDB Source(string configFile) throws IOException ClassNotFoundException props new Properties( props. load(new FileInputstream (configfile)) rl props getProperty( onlyfun caterpillarurl )i user props getProperty ( onlyfun caterpillar user passwd props. getProperty("onlyfun caterpillar password")i Class. forName props. getProperty ("onlyfun caterpillar driver ))i public Connection getConnection() throws SQLException return DriverManager getConnection(url, user, passwd)i public void closeConnection(Connection conn) throws SQLException t conn close()i
简单的Connection工具类别 public class SimpleDBSourceimplementsDBSource { … public SimpleDBSource(String configFile) throws IOException, ClassNotFoundException { props = new Properties(); props.load(new FileInputStream(configFile)); url = props.getProperty("onlyfun.caterpillar.url"); user = props.getProperty("onlyfun.caterpillar.user"); passwd = props.getProperty("onlyfun.caterpillar.password"); Class.forName( props.getProperty("onlyfun.caterpillar.driver")); } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, passwd); } public void closeConnection(Connection conn) throws SQLException { conn.close(); } }
简单的 Connection工具类别 onlyfun. caterpillar. driver=com mysql jdbc Driver onlyfun. caterpillar. url=jdbc: mysql: //localhost: 3306/demo onlyfun. caterpillar.user=root onlyfun caterpillar password=123456 DBSource dbsource new simpleDBSource()i Connection conn =dbsource getConnection ()i f(! conn. isClosedo)( System.out. println("数据库连接已开后…"); dbsource. closeConnection(conn)i if(conn is closed()) System.out. println("数据库连接已矢闭…");
简单的Connection工具类别 onlyfun.caterpillar.driver=com.mysql.jdbc.Driver onlyfun.caterpillar.url=jdbc:mysql://localhost:3306/demo onlyfun.caterpillar.user=root onlyfun.caterpillar.password=123456 DBSource dbsource = new SimpleDBSource(); Connection conn = dbsource.getConnection(); if(!conn.isClosed()) { System.out.println("数据库连接已开启…"); } dbsource.closeConnection(conn); if(conn.isClosed()) { System.out.println("数据库连接已关闭…"); }
简单的连接池( Connectionpoo) 数据库连接的取得是一个耗费时间与资源 的动作 建立 Socket connection 交换数据(用户密码验证、相分参数) 数据库初始会话( Session) 日志( Logging) 分配行程( Process)
简单的连接池(Connectionpool) • 数据库连接的取得是一个耗费时间与资源 的动作 – 建立Socket connection – 交换数据(用户密码验证、相关参数) – 数据库初始会话(Session) – 日志(Logging) – 分配行程(Process) – …