时间:2021-03-22 09:09:58 | 栏目: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);
感谢阅读此文,希望能帮助到大家,谢谢大家对本站的支持!