前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jdbc基础 (一) MySQL的简单使用

jdbc基础 (一) MySQL的简单使用

作者头像
欠扁的小篮子
发布2018-04-11 10:48:23
6990
发布2018-04-11 10:48:23
举报
文章被收录于专栏:技术碎碎念技术碎碎念

前段时间学习了jdbc,正好利用这几篇文章总结一下。

JDBC 可做三件事:与数据库建立连接、发送操作数据库的语句并处理结果。

而程序首先要做的就是加载数据库驱动,这里我使用的是mysql:

代码语言:javascript
复制
1 String driverName=new String("com.mysql.jdbc.Driver");
2 Class.forName(driverName);

然后再获取数据库连接对象,参数为数据库的url,用户名以及密码。这里我使用的数据库名为jdbc,用户名为root,密码为123456:

代码语言:javascript
复制
1 String url=new String("jdbc:mysql://localhost:3306/jdbc");
2 String user=new String("root");
3 String password=new String("123456");
4 Connection coon=DriverManager.getConnection(url, user, password);

因为要对数据库进行操作,所以要获取Statement对象:

代码语言:javascript
复制
1 Statement statement = connection.createStatement();

statement对象内封装的execute(String sql)方法以及executeQuery(String sql)方法和executeUpdate(String sql)方法可以用来执行sql语句,以此来实现对数据库的操作。

代码语言:javascript
复制
 1   String sql = null;
 2   ResultSet resultSe = null;
 3 
 4   //创建Student表
 5   sql="create table Student (id char(9) primary key,name char(9) unique)";
 6   statement.execute(sql);        
 7          
 8   //添加元组
 9   sql = "insert into Student (id,name) values ('0001','zhangsan')";
10   statement.executeUpdate(sql);
11          
12   //查询Student表
13   sql="select * from Student";
14   resultSet = statement.executeQuery(sql);
15             
16   while(resultSet.next()){
17       System.out.println("name:"+resultSet.getString("name"));
18       System.out.println("id:"+resultSet.getString("id"));
19   }
20  
21   //删除元组
22   sql="delete from Student where id='0001'";
23   statement.executeUpdate(sql);
24  
25   //删除表Student
26   sql="drop table Teacher";
27   statement.execute(sql);    

操作完数据库之后要关闭资源,顺序依次为resultSet,statement,connection:

代码语言:javascript
复制
 1 try {
 2     if (resultSet != null)
 3         resultSet.close();
 4  } catch (SQLException e) {
 5        e.printStackTrace();
 6  } finally {
 7        resultSet = null;
 8        try {
 9            if (statement != null)
10              statement.close();
11        } catch (SQLException e) {
12              e.printStackTrace();
13        } finally {
14              statement = null;
15              try {
16                  if (connection != null)
17                     connection.close();
18              } catch (SQLException e) {
19                     e.printStackTrace();
20              } finally {
21                     connection = null;
22              }
23         }
24  }

close()会抛出异常,需要try/catch语句块。为保证资源的释放,需要将close()方法的调用放在finally语句块里,释放资源前判断对象是否为null。至此,利用jdbc连接数据库进行简单的操作算是完成了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-05-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档