侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JDBC 连接MySQL实例详解

数据库  /  管理员 发布于 6年前   290

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);结果集ResultSetwhile(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=JSPBookjdbc-0.proxool.driver-class=com.mysql.jdbc.Driverjdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/sample_db?user=root&password=browser&useUnicode=true&characterEncoding=UTF-8jdbc-0.proxool.maximum-connection-count=10jdbc-0.proxool.prototype-count=4jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATEjdbc-0.proxool.verbose=truejdbc-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);

感谢阅读此文,希望能帮助到大家,谢谢大家对本站的支持!


  • 上一条:
    Mysql精粹系列(精粹)
    下一条:
    mysql 存储过程详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客