简介JDBC JDBC数据库驱动程序依实作方式可以分为 四个类型 Type 1: JDBC-ODBC Bridge Type 2: Native-API Bridge Type 3: JDBC-middleware Type 4: Pure Java Driver 應用程式 應用程式 應用程式 應用程式 JDBC-ODBC Bridge Native-API Bridge JDBC-middleware Pure Java Driver ODBC Driver Native Driver middleware 資料庫 資料庫 資料庫 資料庫
简介JDBC • JDBC数据库驱动程序依实作方式可以分为 四个类型 – Type 1:JDBC-ODBC Bridge – Type 2:Native-API Bridge – Type 3:JDBC-middleware – Type 4:Pure Java Driver
连接数据库 载人JDBC驱动程序 try i Class forName("com mysql jdbc Driver )i catch(ClassNotFoundException e) t System.out. println("找不到驱动程序类别");
连接数据库 • 载入JDBC驱动程序 try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { System.out.println("找不到驱动程序类别"); }
连接数据库 提供 JDBC URL 协定:子协定:数据源识别 jdbc:mysq1://主机名:端口/数据库名称?参数=值&参数=值 jdbc: mysql: //localhost: 3306/demo?user=root&password=123 jdbc: mysql: //localhost: 3306/demo?user=root&password=123& seUnicode=true& characterEncoding=Big 5
连接数据库 • 提供JDBC URL – 协定:子协定:数据源识别 jdbc:mysql://主机名:端口/数据库名称?参数=值&参数=值 jdbc:mysql://localhost:3306/demo?user=root&password=123 jdbc:mysql://localhost:3306/demo?user=root&password=123& useUnicode=true&characterEncoding=Big5
连接数据库 取得 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("数据库连接已关闭…"); }