- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章java基于jdbc连接mysql数据库功能实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了java基于jdbc连接mysql数据库的方法。分享给大家供大家参考,具体如下:
1、JDBC简介 。
Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。它JDBC是面向关系型数据库的.
1、JDBC架构:
JDBC API支持两层和三层处理模型进行数据库访问,但在一般的JDBC体系结构由两层组成:
JDBC API: 提供了应用程序对JDBC的管理连接; 。
JDBC Driver API: 支持JDBC管理到驱动器连接; 。
JDBC API的使用驱动程序管理器和数据库特定的驱动程序提供透明的连接到异构数据库; 。
JDBC驱动程序管理器可确保正确的驱动程序来访问每个数据源,该驱动程序管理器能够支持连接到多个异构数据库的多个并发的驱动程序; 。
以下是结构图,它显示了驱动程序管理器方面的JDBC驱动程序和Java应用程序的位置:
2、常见的JDBC组件:
JDBC API提供了以下接口和类:
DriverManager: 这个类管理数据库驱动程序的列表,内容是否符合从Java应用程序使用的通信子协议正确的数据库驱动程序的连接请求,识别JDBC在一定子协议的第一个驱动器将被用来建立数据库连接; 。
Driver: 此接口处理与数据库服务器通信,很少直接与驱动程序对象,相反,使用DriverManager中的对象,它管理此类型的对象,它也抽象与驱动程序对象工作相关的详细信息; 。
Connection : 此接口与接触数据库的所有方法,连接对象表示通信上下文,即,与数据库中的所有的通信是通过唯一的连接对象; 。
Statement : 可以使用这个接口创建的对象的SQL语句提交到数据库,一些派生的接口接受除执行存储过程的参数; 。
ResultSet: 这些对象保存从数据库后,执行使用Statement对象的SQL查询中检索数据,它作为一个迭代器,让您可以通过移动它的数据; 。
SQLException: 这个类处理发生在一个数据库应用程序的任何错误. 。
2、连接JDBC需要掌握的基本知识 。
1、数据库的基本操作.
eg:Mysql的安装和基本操作(insert,delete,update,query) 。
2、java开发工具的使用, 。
eg:Eclipse/MyEclipse (包括mysql-connector-java-5.0.3-bin.jar的导入) 。
3、JDBC的连接及代码演示 。
1、JDBC连接工具类 。
1)、Configuration.java:可以从.xml文件中连接数据库的配置信息,需要引入dom4j-1.6.1.jar包 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package
cn.java.jdbc;
import
java.io.InputStream;
import
org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.Element;
import
org.dom4j.io.SAXReader;
public
class
Configuration {
private
String url;
private
String driver;
private
String username;
private
String password;
public
Configuration() {
}
public
Configuration(String url, String driver, String username,
String password) {
super
();
this
.url = url;
this
.driver = driver;
this
.username = username;
this
.password = password;
}
public
static
Configuration getConfigure()
{
try
{
InputStream in = Configuration.
class
.getResourceAsStream(
"/db.xml"
);
if
(
null
!=in) {
return
load(in);
}
return
null
;
}
catch
(DocumentException e) {
e.printStackTrace();
return
null
;
}
}
private
static
Configuration load(InputStream in)
throws
DocumentException {
SAXReader reader =
new
SAXReader();
Document doc = reader.read(in);
Element jdbc = doc.getRootElement();
String url = jdbc.element(
"url"
).getText();
String driver = jdbc.element(
"driver"
).getText();
String username = jdbc.element(
"username"
).getText();
String password = jdbc.element(
"password"
).getText();
Configuration cfg =
new
Configuration(url, driver, username, password);
return
cfg;
}
public
String getUrl() {
return
url;
}
public
void
setUrl(String url) {
this
.url = url;
}
public
String getDriver() {
return
driver;
}
public
void
setDriver(String driver) {
this
.driver = driver;
}
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
}
|
2)、db.xml:保存数据库的配置信息 。
1
2
3
4
5
6
7
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
jdbc
>
<
url
>jdbc:mysql://localhost:3306/test</
url
>
<
driver
>com.mysql.jdbc.Driver</
driver
>
<
username
>root</
username
>
<
password
></
password
>
</
jdbc
>
|
3)、ConnectionFactory.java:JDBC连接工厂方法之一 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package
cn.java.jdbc;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.SQLException;
public
class
ConnectionFactory {
private
static
ConnectionFactory connectionFactory=
null
;
private
static
Configuration config=Configuration.getConfigure();
private
ConnectionFactory()
{
try
{
Class.forName(config.getDriver());
}
catch
(ClassNotFoundException e) {
}
}
public
Connection getConnection()
throws
SQLException
{
Connection con=
null
;
try
{
con=DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
return
con;
}
finally
{
// if (null != con) {
// con.close();
// }
}
}
public
static
ConnectionFactory getInstance()
{
if
(
null
==connectionFactory) {
connectionFactory=
new
ConnectionFactory();
}
return
connectionFactory;
}
}
|
4)、ConnectionFactory2.java:JDBC连接工厂方法之二 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package
cn.java.jdbc;
import
java.sql.Connection;
import
java.sql.SQLException;
import
javax.sql.DataSource;
import
com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public
class
ConnectionFactory2 {
private
DataSource ds ;
private
static
ConnectionFactory2 connectionFactory2 =
null
;
private
ConnectionFactory2() {
MysqlDataSource myDS =
new
MysqlDataSource() ;
myDS.setServerName(
"localhost"
);
myDS.setDatabaseName(
"test"
);
myDS.setPort(
3306
) ;
myDS.setUser(
"root"
);
myDS.setCharacterEncoding(
"utf-8"
);
myDS.setPassword(
""
);
this
.ds = myDS ;
}
public
Connection getConnection()
throws
SQLException {
Connection conn =
null
;
try
{
conn = ds.getConnection() ;
conn.setAutoCommit(
false
);
return
conn;
}
catch
(SQLException e) {
if
(
null
!= conn) {
conn.close();
}
throw
e;
}
}
public
static
ConnectionFactory2 getInstance() {
if
(
null
== connectionFactory2) {
connectionFactory2 =
new
ConnectionFactory2();
}
return
connectionFactory2;
}
}
|
5)、User.java:定义数据库表user中的id和name的bean类,其中id是自动增长的 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package
cn.java.jdbc;
public
class
User {
private
int
id;
private
String name;
public
User() {
}
public
User(
int
id, String name) {
this
.id = id;
this
.name = name;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
}
|
6)、UserDao.java:user表的操作类,实现了insert、delete、update、query等方法 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package
cn.java.jdbc;
import
java.io.IOException;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.List;
public
class
UserDao {
private
PreparedStatement st =
null
;
private
ResultSet rs =
null
;
public
UserDao() {
}
public
void
insert( Connection con,String name)
throws
SQLException,IOException {
String sql=
"insert into user(name) values(?) "
;
try
{
st=con.prepareStatement(sql);
st.setString(
1
, name);
st.executeUpdate();
}
finally
{
if
(
null
!=st) {
st.close();
}
}
}
public
void
delete(Connection con,
int
id)
throws
SQLException,IOException {
String sql=
"delete from user where id=?"
;
try
{
st=con.prepareStatement(sql);
st.setInt(
1
, id);
st.executeUpdate();
}
finally
{
if
(
null
!=st) {
st.close();
}
}
}
public
void
update( Connection con,
int
id,String name)
throws
SQLException,IOException {
String sql=
"update user set name=? where id=?"
;
try
{
st=con.prepareStatement(sql);
st.setString(
1
, name);
st.setInt(
2
, id);
st.executeUpdate();
}
finally
{
if
(
null
!=st) {
st.close();
}
}
}
public
User query(Connection con,
int
index)
throws
SQLException,IOException{
User user=
new
User();
String sql=
"select * from user where id=?"
;
try
{
st=con.prepareStatement(sql);
st.setInt(
1
, index);
rs=st.executeQuery();
while
(rs.next()) {
user.setId(rs.getInt(
1
));
user.setName(rs.getString(
2
));
break
;
}
}
finally
{
if
(
null
!=rs) {
rs.close();
}
if
(
null
!=st) {
st.close();
}
}
return
user;
}
public
List<User> queryAll(Connection con)
throws
SQLException,IOException {
List<User> list=
new
ArrayList<>();
String sql=
"select * from user"
;
try
{
st=con.prepareStatement(sql);
rs=st.executeQuery();
while
(rs.next()) {
User user=
new
User();
user.setId(rs.getInt(
1
));
user.setName(rs.getString(
2
));
list.add(user);
}
}
finally
{
if
(
null
!=rs) {
rs.close();
}
if
(
null
!=st) {
st.close();
}
}
return
list;
}
}
|
2、JDBC连接的测试类 。
1)、TestJdbc.java:数据库测试类 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package
cn.java.jdbc;
import
java.io.IOException;
import
java.sql.Connection;
import
java.sql.SQLException;
import
java.util.List;
public
class
TestJdbc {
public
static
void
main(String[] args) {
Connection con=
null
;
try
{
con=(Connection) ConnectionFactory.getInstance().getConnection();
UserDao userDao=
new
UserDao();
//con=(Connection) ConnectionFactory2.getInstance().getConnection();
if
(
null
!=con) {
System.out.println(
"Link JDBC SUCESS"
);
//userDao.insert(con, "zhangsir");
//userDao.delete(con, 4);
//userDao.update(con, 1, "david");
List<User> list=userDao.queryAll(con);
for
(User user : list) {
System.out.println(
"id="
+user.getId()+
" name="
+user.getName());
}
}
}
catch
(SQLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
if
(
null
!=con) {
try
{
con.close();
}
catch
(SQLException e) {
}
}
}
}
}
|
3、JDBC连接总结 。
JDBC操作的基本步骤是:
1、创建Connection对象,传入SQL查询命令字符串; 2、获取PreparedStatement对象:通过Connection对象传入SQL查询命令获取; 3、获取ResultSet:对PreparedStatement执行executeUpdate()或者executeQurey()获取; 4、依次关闭打开的对象:先后关闭ResultSet、PreparedStatement和Connection对象. 。
希望本文所述对大家java程序设计有所帮助.
原文链接:http://blog.csdn.net/j086924/article/details/51546876 。
最后此篇关于java基于jdbc连接mysql数据库功能实例详解的文章就讲到这里了,如果你想了解更多关于java基于jdbc连接mysql数据库功能实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!