java从入门到弃坑数据库终

来源:互联网 发布:好的炒作公司网络推手 编辑:程序博客网 时间:2024/06/06 08:52

1.JDBC进行批处理:为了提高sql语句发送到数据库的效率,运用批处理建立sql缓存区,一次发送多条sql到数据库

                成员方法:void addBatch(String sql) 添加到sql缓存区(暂时不发送)

                                     int[] executeBatch()执行批处理命令。发送所有缓存区的sql

                                     void clearBatch() 清空sql缓存区

用properties集合创建增强jdbc工具类

import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class EXJDBCUtil {private static String url=null;private static String user=null;private static String password=null;private static String classname=null;static {try {Properties prop=new Properties();prop.load(new FileInputStream("db.properties"));url=prop.getProperty("url");user=prop.getProperty("user");password=prop.getProperty("password");classname=prop.getProperty("classname");Class.forName(classname);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}public static Connection getconn(){try {Connection conn=DriverManager.getConnection(url, user, password);return conn;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();throw new RuntimeException();}}public static void close(Connection conn,Statement stmt,ResultSet rs){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stmt!=null){try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
不用批处理插入数据:

private static void teststatement() {// TODO Auto-generated method stubConnection conn=null;Statement stmt=null;try {conn=EXJDBCUtil.getconn();stmt=conn.createStatement();for (int i = 0; i < 2000; i++) {String sql="INSERT INTO stu VALUES ("+i+",'a');";stmt.execute(sql);}} catch (Exception e) {// TODO: handle exception}finally{EXJDBCUtil.close(conn, stmt, null);}

使用批处理插入数据:
private static void teststatementbatch() {// TODO Auto-generated method stubConnection conn=null;Statement stmt=null;try {conn=EXJDBCUtil.getconn();stmt=conn.createStatement();for (int i = 0; i < 2000; i++) {String sql="insert into stu values("+i+",'刘德华');";stmt.addBatch(sql);if(i%20==0){stmt.executeBatch();stmt.clearBatch();}}} catch (Exception e) {// TODO: handle exception}finally{EXJDBCUtil.close(conn, stmt, null);}}

2.jdbc获取自增长值:
             方法:Statement.RETURN_GENERATED_KEYS:可以返回自动增长值
                   Statement.NO_GENERATED_KEYS:不能返回自动增长值
             在用conn的preparedstatement方法获得stmt对象时在对应位置插入方法即可
stmt = conn.prepareStatement(deptSql, Statement.RETURN_GENERATED_KEYS);
3.jdbc处理大数据文件:分为造作字符文件和字节文件,再次运用io流即可通过java实现文件和
                      数据库之间的数据传输,再次要对clob进行操作。
exp:注:根据文件大小不同,所选择数据库对应格式也不同
import java.io.FileInputStream;public class BlobDemo {public static void main(String[] args) {write();//向数据库写入文件read();//从数据库读取文件}private static void read() {// TODO Auto-generated method stubConnection conn=null;PreparedStatement stmt=null;ResultSet rs=null;try {conn=EXJDBCUtil.getconn();String sql="select * from stu where id=?;";//创建sql预编译语句stmt=conn.prepareStatement(sql);//进行sql预编译stmt.setInt(1, 1);//第一个是语句中问号的位置,第二个是对问号赋的值rs=stmt.executeQuery();while(rs.next()){//创建io流对象,循环读取所得文件数据InputStream is=rs.getBinaryStream(2);FileOutputStream fos=new FileOutputStream("b.jpg");byte[] chs=new byte[1024];int len;while((len=is.read(chs))!=-1){fos.write(chs, 0, len);}fos.close();//关流is.close();}} catch (Exception e) {// TODO: handle exception}finally{EXJDBCUtil.close(conn, stmt, rs);}}private static void write() {// TODO Auto-generated method stubConnection conn=null;PreparedStatement stmt=null;try {conn=EXJDBCUtil.getconn();String sql="insert into stu values(?,?);";stmt=conn.prepareStatement(sql);stmt.setInt(1, 1);//选择输入流文件发送到数据库stmt.setBlob(2, new FileInputStream("beyonetta.jpg"));int count=stmt.executeUpdate();System.out.println(count);} catch (Exception e) {// TODO: handle exception}finally{EXJDBCUtil.close(conn, stmt, null);}}}
4.数据库事务:把多条sql语句绑定在一起,要么一起成功,要么一起失败。
         sql指令:SET autocommit=0/1;1为开启自动提交,0为关闭。
                        COMMIT  提交事务,提交后不能回滚。
                        ROLLBACK  回滚到事务开始前
       jdbc指令:
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import com.edu_01.EXJDBCUtil;//模拟转账操作,一人扣钱,一人加钱,同时成功或失败public class trabsation {public static void main(String[] args) {Connection conn=null;PreparedStatement stmt=null;String sql1="update account set balance=balance-2000 where name='james';";String sql2="update account set balance=balance+2000 where name='weide';";try {conn=EXJDBCUtil.getconn();conn.setAutoCommit(false);//关闭自动提交stmt=conn.prepareStatement(sql1);stmt.executeUpdate();//发送语句1stmt=conn.prepareStatement(sql2);stmt.executeUpdate();//发送语句2conn.commit();//提交事务} catch (Exception e) {// TODO: handle exceptiontry {conn.rollback();//若发生异常进行回滚} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}finally{//释放资源EXJDBCUtil.close(conn, stmt, null);}}}
5.事务的特性:A:原子性,要么一起成功,要么一起失败
                        B:一致性,数据库从一个一致性的状态到另一个一致性的状态,保持不变
                        C:隔离性,多个并发事务可以相互隔离
                        D:永久性,事务一旦提交,便永久保存





原创粉丝点击