jdbc


  1. package com.lcx.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Savepoint;  
  9. import java.util.Properties;  
  10.   
  11. public class Test {  
  12.     public static void main(String[] args) {  
  13.         String url1="jdbc:mysql://localhost:3306/JAVA?user=root&password=root&useUnicode=true&charactorEncoding=UTF8";  
  14.         String url2="jdbc:mysql://localhost:3306/JAVA?useUnicode=true&charactorEncoding=UTF8";  
  15.         String url3="jdbc:mysql://localhost:3306/JAVA";  
  16.         String user="root";  
  17.         String password="root";  
  18.         Connection conn3 = null;  
  19.         Savepoint point1 = null;  
  20.           
  21.         try {  
  22.             //1、加载数据库驱动,包名一般为域名反写  
  23.             Class.forName("com.mysql.jdbc.Driver");  
  24.             Properties info = new Properties();  
  25.             info.put("user""root");  
  26.             info.put("password""root");  
  27.             info.put("useUnicode","true");  
  28.             info.put("charactorEncoding","utf8");  
  29.             /* 
  30.              * 2、获取数据连接对象,DriverManager.getConnection方法有3个重载方法 
  31.              */  
  32. //          Connection conn1 = DriverManager.getConnection(url3, user, password);//将连接的用户名、密码放到方法参数中  
  33. //          Connection conn2 = DriverManager.getConnection(url2,info);//将连接的属性键值对放在Properties对象中  
  34.             conn3 = DriverManager.getConnection(url1);//将所有连接信息都放在URL中  
  35.             /* 
  36.              * 3、根据SQL获取sql会话对象,有2种方式 Statement、PreparedStatement 
  37.              *      1、 PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象。 
  38.              *      2、作为 Statement 的子类,PreparedStatement 继承了 Statement 的所有功能。三种方法  execute、 executeQuery 和 executeUpdate 已被更改以使之不再需要参数 
  39.              *      3、在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替 Statement.也就是说,在任何时候都不要使用Statement. 
  40.              *          一.代码的可读性和可维护性.Statement需要不断地拼接,而PreparedStatement不会。 
  41.              *          二.PreparedStatement尽最大可能提高性能.DB有缓存机制,相同的预编译语句再次被调用不会再次需要编译。 
  42.              *          三.最重要的一点是极大地提高了安全性.Statement容易被SQL注入,而PreparedStatementc传入的内容不会和sql语句发生任何匹配关系。 
  43.              * 最常用的api 
  44.              * addBatch()/addBatch(String sql) 预编译SQL语句,只编译一回哦,效率高啊。剥瓜子,一个一个剥,最后一口吃。 
  45.              * setXXX(parameterIndex,value) 设置指定参数的值。 
  46.              * execute()  执行 SQL 语句,该语句可以是任何种类的 SQL 语句。 
  47.              * executeQuery() 执行查询语句返回ResultSet 
  48.              * executeUpdate() 执行增删改,返回影响的行数。 
  49.              *  
  50.              */  
  51.             String sql_other = "TRUNCATE TABLE t_user_info";  
  52.             String sql_insert = "insert into t_user_info(id,user,sex,age) value(null,?,?,?)";  
  53.             String sql_query = "select * from t_user_info where id < ?";  
  54.             conn3.setAutoCommit(false);//关闭自动提交  
  55.             PreparedStatement preparedStatement_other = conn3.prepareStatement(sql_other);  
  56.             PreparedStatement preparedStatement_insert = conn3.prepareStatement(sql_insert);  
  57.             PreparedStatement preparedStatement_query = conn3.prepareStatement(sql_query);  
  58.             preparedStatement_other.execute();  
  59.               
  60.               
  61.             for(int i=0;i<100;i++){  
  62.                 preparedStatement_insert.setString(1"user_"+i);  
  63.                 preparedStatement_insert.setString(2"n");  
  64.                 preparedStatement_insert.setInt(3, i);  
  65.                 if(i>50){  
  66. //                  preparedStatement_insert.setString(3, "我是错误的尝试");  
  67.                 }  
  68.                 preparedStatement_insert.addBatch();  
  69.             }  
  70.             //一次性插入100条记录,如果中间有出错,那么这一次性的插入都不会成功  
  71.             int[] updateInt = preparedStatement_insert.executeBatch();  
  72.             conn3.commit();  
  73.             point1 = conn3.setSavepoint("point1");  
  74.             System.out.println(updateInt.length);  
  75.               
  76.             preparedStatement_query.setInt(150);  
  77.             //4、执行SQL语句,查询语句就获取结果集  
  78.             ResultSet result = preparedStatement_query.executeQuery();  
  79.             while(result.next()){  
  80.                 int a = result.getInt("id");  
  81.                 int b = result.getInt(1);  
  82.                 System.out.println("第一列值,通过列名:"+a+",通过下标:"+b);  
  83.                   
  84.             }  
  85.             /* 
  86.              * 5、关闭结果集、关闭会话、关闭连接 
  87.              */  
  88.             result.close();  
  89.             preparedStatement_other.close();  
  90.             preparedStatement_insert.close();  
  91.             preparedStatement_query.close();  
  92.             conn3.close();  
  93.               
  94.         } catch (ClassNotFoundException e) {  
  95.             System.out.println("驱动没有加载到。。。");  
  96.             e.printStackTrace();  
  97.         } catch (SQLException e) {  
  98.             System.out.println("出现sql异常。。。");  
  99.             try {  
  100.                 conn3.rollback(point1);;  
  101.             } catch (SQLException e1) {  
  102.                   
  103.                 e1.printStackTrace();  
  104.             }  
  105.             e.printStackTrace();  
  106.         }  
  107.     }  
  108. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值