JDBC 銜接MySQL實例詳解。本站提示廣大學習愛好者:(JDBC 銜接MySQL實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是JDBC 銜接MySQL實例詳解正文
JDBC銜接MySQL
JDBC銜接MySQL
加載及注冊JDBC驅動法式
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
JDBC URL 界說驅動法式與數據源之間的銜接
尺度語法:
<protocol(重要通信協定)>:<subprotocol(主要通信協定,即驅動法式稱號)>:<data source identifier(數據源)>
MySQL的JDBC URL格局:
jdbc:mysql//[hostname][:port]/[dbname][?param1=value1][¶m2=value2]….
示例:jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
罕見參數:
user 用戶名
password 暗碼
autoReconnect 聯機掉敗,能否從新聯機(true/false)
maxReconnect 測驗考試從新聯機次數
initialTimeout 測驗考試從新聯機距離
maxRows 傳回最年夜行數
useUnicode 能否應用Unicode字體編碼(true/false)
characterEncoding 何種編碼(GB2312/UTF-8/…)
relaxAutocommit 能否主動提交(true/false)
capitalizeTypeNames 數據界說的稱號以年夜寫表現
樹立銜接對象
String url="jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";
Connection con = DriverManager.getConnection(url);
樹立SQL陳說式對象(Statement Object)
Statement stmt = con.createStatement();
履行SQL語句
executeQuery() String query = "select * from test"; ResultSet rs=stmt.executeQuery(query); 成果集ResultSet while(rs.next()) {rs.getString(1);rs.getInt(2);} executeUpdate() String upd="insert into test (id,name) values(1001,xuzhaori)"; int con=stmt.executeUpdate(upd); execute()
示例:
try { } catch(SQLException sqle) { } finally { }
Java類型和SQL類型 技巧手冊P421
PreparedStatement(預編語句)
PreparedStatement stmt = conn.prepareStatement("insert into test(id,name)values(?,?)");
stmt.setInt(1,id);
stmt.setString(2,name);
注:一旦設定語句的參數值後,便可以屢次履行改語句,直到挪用clearParameters()辦法將他消除為止
CallableStatement(預儲法式)技巧手冊P430
JDBC2.0應用
ResultSet對象中的光標高低自在挪動
Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select * from test");
public Statement createStatement(int resultSetType,int resultSetConcuttency) throws SQLException
resultSetType
TYPE_FORWARD_ONLY 只能應用next()辦法。
TYPE_SCROLL_SENSITIVE 可以高低挪動,可以獲得轉變後的值。
TYPE_SCROLL_INSENSITIVE 可以高低挪動。
resultSetConcuttency
CONCUR_READ_ONLY 只讀
CONCUR_UPDATABLE ResultSet對象可以履行數據庫的新增、修正、和移除
直接應用ResultSet對象履行更新數據
新增數據
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_PUDATABLE); ResultSet uprs=stmt.executeQuery("select * from test"); uprs.moveToInsertRow(); uprs.updateInt(1,1001); uprs.updateString(2,"許召日"); uprs.insertRow;
更新數據
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_PUDATABLE); ResultSet uprs=stmt.executeQuery("select * from test"); uprs.last(); uprs.updateString("name","xuzhaori"); uprs.updateRow;
刪除數據
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_PUDATABLE); ResultSet uprs=stmt.executeQuery("select * from test"); uprs.absolute(4); uprs.deleteRow();
批處置
con.setAutoCommit(false); 封閉主動承認形式 Statement stmt=con.createStatement(); int[] rows; stmt.addBatch("insert into test values(1001,xuzhaori)"); stmt.addBatch("insert into test values(1002,xuyalin)"); rows=stmt.executeBatch(); con.commit(); 沒有任何毛病,履行批處置stmt.executeBatch();
JNDI-數據源(Data Source)與銜接池(Connection Pool)
Tomcat的JDBC數據源設置 技巧手冊P439
銜接池對象-Proxool Var 0.8.3 技巧手冊P446
設置web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <!--<?xml version="1.0" encoding="GB2312"?>--> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> …. <servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param> <param-name>propertyFile</param-name> <param-value>WEB-INF/classes/Proxool.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 後端統計端口添加以下 <servlet> <servlet-name>Admin</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/Admin</url-pattern> </servlet-mapping> …. </web-app>
設置裝備擺設Proxool.properties
jdbc-0.proxool.alias=JSPBook jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/sample_db?user=root&password=browser&useUnicode=true&characterEncoding=UTF-8 jdbc-0.proxool.maximum-connection-count=10 jdbc-0.proxool.prototype-count=4 jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE jdbc-0.proxool.verbose=true jdbc-0.proxool.statistics=10s,1m,1d 後端統計接口添加此行 jdbc-0.proxool.statistics-log-level=DEBUG
應用Proxool銜接池
Connection con = DriverManager.getConnection("proxool.JSPBook"); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String query = "SELECT * FROM employee"; ResultSet rs = stmt.executeQuery(query);
感激浏覽此文,願望能贊助到年夜家,感謝年夜家對本站的支撐!