专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 JDBC连接MySQL详解

JDBC连接MySQL详解

更新时间:2021-01-22 17:39:24 来源:星辉 浏览970次

Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法,JDBC主要就是与数据库建立连接,然后发送 SQL 语句,处理结果。其中JDBC连接数据库的典型代表就是MySQL数据库,本文我们就来介绍JDBC连接MySQL的详尽过程。

 

1.加载及注册JDBC驱动程序

Class.forName("com.mysql.jdbc.Driver");

Class.forName("com.mysql.jdbc.Driver").newInstance();

 

 

2.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        数据定义的名称以大写表示

 

3.建立连接对象

String url="jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";

Connection con = DriverManager.getConnection(url);

 

4.建立SQL陈述式对象(Statement Object)

Statement stmt = con.createStatement();

 

5.执行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

{

}

 

6.Java类型和SQL类型

PreparedStatement(预编语句)

PreparedStatement stmt = conn.prepareStatement("insert into test(id,name)values(?,?)");

stmt.setInt(1,id);

stmt.setString(2,name);

注:一旦设定语句的参数值后,就可以多次执行改语句,直到调用clearParameters()方法将他清除为止

CallableStatement(预储程序)

 

7.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对象可以执行数据库的新增、修改、和移除

 

8.直接使用ResultSet对象执行更新数据

1)新增数据

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;

2)更新数据

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;

3)删除数据

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_PUDATABLE);

ResultSet uprs=stmt.executeQuery("select * from test");

uprs.absolute(4);

uprs.deleteRow();

 

9.批处理

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();

 

 

JDBC为程序员指定了一组在编写SQL请求时使用的面向对象的类。还有一组附加的类描述了JDBC驱动API。能映射成Java数据类型的最普通的SQL数据类型都是支持的。这个API提供了微软事务服务器请求的执行支持以及提交和回滚到事务开始的能力。想要学习更多的JDBC连接MySQL的知识,快来本站的MySQL教程学习吧!

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>