JDBC的应用
public static Connection getConnection(String url,
String user,
String password)
throws SQLException
- 试图建立到给定数据库 URL 的连接。
DriverManager试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。 -
- 参数:
url-jdbc:subprotocol:subname形式的数据库 urluser- 数据库用户,连接是为该用户建立的password- 用户的密码- 返回:
- 到 URL 的连接
- 抛出:
SQLException- 如果发生数据库访问错误- Connection接口不能够自己new对象,DriverManager中有getConnection可以创建对象
createStatement
Statement createStatement()
throws SQLException
- 创建一个
Statement对象来将 SQL 语句发送到数据库。不带参数的 SQL 语句通常使用Statement对象执行。如果多次执行相同的 SQL 语句,使用PreparedStatement对象可能更有效。使用返回的
Statement对象创建的结果集在默认情况下类型为TYPE_FORWARD_ONLY,并带有CONCUR_READ_ONLY并发级别。已创建结果集的可保存性可调用getHoldability()确定。 -
- 返回:
- 一个新的默认
Statement对象 - 抛出:
SQLException
-
- - 如果发生数据库访问错误,或者在关闭的连接上调用此方法
----------------------------------------------------------------------------------------------------------------------------------------
- ResultSet接口:
- 1.表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
2.ResultSet对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next方法将光标移动到下一行;因为该方法在ResultSet对象没有下一行时返回false,所以可以在while循环中使用它来迭代结果集。- 查找数据表代码:
try {Class.forName("com.mysql.jdbc.Driver");String url= "jdbc:mysql://localhost:3306/test_1";String username = "root";String password = "Boyce";Connection conn = DriverManager.getConnection(url, username, password);String sql="select*from tb_books where name=? and author=?";// Statement statement= conn.createStatement();PreparedStatement statement =conn.prepareStatement(sql);//通过Statement与数据库交互statement.setInt(1, 33); //1代表sql语句中从左边数第一个问号,22代表sql语句中的name的值statement.setString(2,"33");//两个参数时,必须在同一行,因为要指出同一个数值ResultSet rs = statement.executeQuery(); //执行静态SQL语句,并返回结果//解析resultSet结果集,所接收的返回结果//ResultSet 对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next 方法将光标移动到下一行;//因为该方法在 ResultSet 对象没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集。while(rs.next()){// System.out.println(rs.getString(1)); //括号中的数值代表某一类属性值,也可以说是一列数值System.out.println(rs.getString("author"));System.out.println(rs.getString(2));}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}
- ----------------------------------------------------------------------------------------------------------------------------------------
JDBC插入数据表记录Class.forName("com.mysql.jdbc.Driver");String url= "jdbc:mysql://localhost:3306/test_1";String username = "root";String password = "Boyce";Connection conn = DriverManager.getConnection(url, username, password);String sql ="insert into tb_books values(?,?,?,?)";PreparedStatement statement =conn.prepareStatement(sql);//通过Statement与数据库交互 多个参数statement.setString(1,"cyuyan");statement.setDouble(2, 100);statement.setString(3, "20");statement.setString(4, "lijianwei");int rs = statement.executeUpdate(); //是executeUpdate()而不是executeQuery()if(rs!=0){ //返回整数,如果re==1则插入成功,返回值为0则插入失败System.out.println("插入成功");}else{System.out.println("插入时报");}----------------------------------------------------------------------------------------------------------------------------------------JDBC更新数据表记录Class.forName("com.mysql.jdbc.Driver");String url= "jdbc:mysql://localhost:3306/test_1";String username = "root";String password = "Boyce";Connection conn = DriverManager.getConnection(url, username, password);String sql = "update tb_books set name=?where author='lijianwei'"; //问号设置为math 根据author为lijianwei去查找,若author是String类型则注意要加上单引号PreparedStatement statement =conn.prepareStatement(sql);//通过Statement与数据库交互 多个参数statement.setString(1,"cyuyan");int rs = statement.executeUpdate();if(rs!=0){System.out.println("插入成功");}else{System.out.println("插入时报");}----------------------------------------------------------------------------------------------------------------------------------------
JDBC批量插入:
public class JDBCTest1 {private static String url= "jdbc:mysql://localhost:3306/test_1";private static String username = "root";private static String password = "Boyce";/*** 载入驱动类*/static{try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 获取连接对象* @return* @throws SQLException*/public static Connection getconection() throws SQLException{return DriverManager.getConnection(url, username, password);}//释放资源public static void free(Connection conn,Statement statement,ResultSet rs){try {if(rs!=null)rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{ //不管rs是否关闭成功,都会执行finally块try {if(statement!=null)statement.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(conn!=null)conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}public class JDBCTest2 {/*** 查询表记录* @param sql* @throws SQLException*/public void queryAll(String sql) throws SQLException{Connection conn = JDBCTest1.getconection(); //不需要new对象调用,JAVA中同一个包内的方法可通过类名直接调用Statement statement = conn.createStatement();ResultSet rs = statement.executeQuery(sql);//解析rs内容while(rs.next()){System.out.println(rs.getString(1)); //括号中的数值代表某一类属性值,也可以说是一列数值System.out.println(rs.getString("author"));System.out.println(rs.getString(2));}JDBCTest1.free(conn, statement, rs);}public void isertBatch() throws SQLException{Connection conn = JDBCTest1.getconection();conn.setAutoCommit(false); //不自动提交,因为如果有一个失败时,可以整体回滚String sql ="insert into tb_books values(?,?,?,?)";PreparedStatement statement = conn.prepareStatement(sql);for(int i=1;i<5;i++){statement.setString(1, "boyce"+i);statement.setDouble(2, 100+i);statement.setString(3, "i"+i);statement.setString(4, "boyce");statement.addBatch();}statement.executeBatch(); //同一提交给数据库conn.commit(); //注意提交事务JDBCTest1.free(conn, statement, null);System.out.println("批量插入成功");}}public static void main(String[] args) throws SQLException {JDBCTest2 test2= new JDBCTest2();try {test2.isertBatch();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} - 查找数据表代码:
**注意:如果是sql是查询语句,返回的是结果集,使用ResultSet去接收,若sql是插入或更新数据返回的则是int类型
PreparedStatement可以带参数,也可以不带参数
JDBC的应用的更多相关文章
- Java数据库连接技术——JDBC
大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- JDBC MySQL 多表关联查询查询
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...
- JDBC增加删除修改
一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...
- JDBC简介
jdbc连接数据库的四个对象 DriverManager 驱动类 DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...
- JDBC Tutorials: Commit or Rollback transaction in finally block
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...
- FineReport如何用JDBC连接阿里云ADS数据库
在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...
- JDBC基础
今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...
- Spring学习记录(十四)---JDBC基本操作
先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...
随机推荐
- jquery mobile 图片自适应问题
解决办法: 加入一段css <link rel="stylesheet" href="http://jquerymobile.com/demos/1.1.0/doc ...
- 服务器后台TCP连接存活问题
0. 背景 公司的服务器后台部署在某一个地方,接入的是用户的APP,而该地方的网络信号较差,导致了服务器后台在运行一段时间后用户无法接入,那边的同事反馈使用netstat查看系统,存在较多的TCP连接 ...
- MSSQL日期格式化
Sql Server 中一个非常强大的日期格式化函数 Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CON ...
- Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) (转)
git pull的时候遇到 Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) y 于 ...
- _fastcall
* 1楼 __fastcall具体含义 在C语言中,假设我们有这样的一个函数: int function(int a,int b) 调用时只要用result = function(1,2)这样的方式就 ...
- 移动WebApp利用Chrome浏览器进行调试
详细的请看这个(HBuilder是我长期使用,而且值得支持的国内前端开发编辑器) http://ask.dcloud.net.cn/article/151 http://ask.dcloud.net. ...
- vs2010 中无法打开 源文件 "stdafx.h" 未定义标识符 “xxx”
解决方案: 项目属性->配置属性->C/C++->常规->附加包含目录->$(ProjectDir)
- glusterFS的常用命令 (转)
1. 启动/关闭/查看glusterd服务 # /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/g ...
- C#2.0 特性
泛型 迭代器 分布类 可空类型 匿名方法 命名空间别名限定符 静态类 外部程序程序集别名 属性访问器可访问性 委托中的协变和逆变 如何声明.实例化.使用委托 固定大小的缓冲区 友元程序集 内联警告控制 ...
- 认识ATL窗口
这是一个相当于“Hello world!”的任务,作为认识ATL,考查了其运作流程与机制. 环境:VS2008 创建:新建-项目-Win32项目-添加公用头文件用于(选择ATL). PS:注意新建项目 ...