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 ...
随机推荐
- nodeJS接受post传过来的参数
1.nodeJs接受Post传递的参数需要通过绑定两个事件来获取, querystring = require("querystring"); 1 app.post('/comm ...
- php ceil() 函数向上舍入为最接近的整数。
代码: <?php echo(ceil(0.60); echo(ceil(0.40); echo(ceil(); echo(ceil(5.1); echo(ceil(-5.1); echo(ce ...
- Linux python <tab>自动补全
为Python添加交互模式下TAB自动补全以及命令历史功能. 1.获取python目录 [root@localhost ~]# python Python 2.6.6 (r266:84292, Jul ...
- 做技术最自由,在IT最幸福!
这些天来,一直感觉"做技术最自由,在IT最幸福!" 在IT最幸福 一直感觉从事IT行业最幸福,想想那些干机械的.干汽修.地勤的,让我干那些工作,对我来说真是折磨! 大体总结以下几点 ...
- jquery的load和get的区别
jquery的load把返回的数据放到指定的元素中,不是全局函数:jquery的get把返回的数据交给用户处理,是全局函数. load和get同样是jquery的ajax函数,load的实现,几乎等于 ...
- xml基础总结
可扩展的标记语言(eXtensible Markup Language) 优点:容易读懂:格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎的编写. 用普通二进制传输数据的缺点,解析方式 ...
- HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
var divs = html.CssSelect("div"); //all div elementsvar nodes = html.CssSelect("div. ...
- mapreduce 自定义数据类型的简单的应用
本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...
- fatal: Paths with -a does not make sense.
git commit -am '*屏蔽设置缓存' htdocs/s.php fatal: Paths with -a does not make sense. 应该用下面的这样. git commit ...
- 修改emlog表字段名称
在em_twitter表中增加一个字段. ,添加一个字段isImportant alter table em_twitter add isImprotant ) not ; ,把字段isImprota ...