java从入门到弃坑数据库终

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 exception
			e.printStackTrace();
		}
	}
	public static Connection getconn(){
		try {
			Connection conn=DriverManager.getConnection(url, user, password);
			return conn;
		} catch (Exception e) {
			// TODO: handle exception
			e.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 block
				e.printStackTrace();
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
不用批处理插入数据:

	private static void teststatement() {
		// TODO Auto-generated method stub
		Connection 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 stub
		Connection 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 stub
		Connection 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 stub
		Connection 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();//发送语句1
			stmt=conn.prepareStatement(sql2);
			stmt.executeUpdate();//发送语句2
			conn.commit();//提交事务
			
		} catch (Exception e) {
			// TODO: handle exception
			try {
				conn.rollback();//若发生异常进行回滚
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{//释放资源
			EXJDBCUtil.close(conn, stmt, null);
		}
	}
}
5.事务的特性:A:原子性,要么一起成功,要么一起失败
                        B:一致性,数据库从一个一致性的状态到另一个一致性的状态,保持不变
                        C:隔离性,多个并发事务可以相互隔离
                        D:永久性,事务一旦提交,便永久保存





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值