【Java web基础】JDBC基础

JDBC使用步骤(比较经典和基础的实现方式):

一、statement

  public abstract interface java.sql.Statement extends java.sql.Wrapper, java.lang.AutoCloseable

首先要清楚statement是一个接口

a.statement的update(增删改)

1.导入驱动包,加载具体的驱动类;

Class.forName("oracle.jdbc.OracleDriver");

2.和数据库建立链接;

connection = DriverManager.getConnection(URL, USERNAME, PWD);

3.发送SQL语句(增删改->update,查->query);

增删改 int count = statement.executeUpdate(sql);

查        int

statement = connection.createStatement();

4.返回结果(由于需要close,一般都是try,catch捕获异常,并把close写入finally)

String sql = "insert into student2 values(2,'zs',22,'g4')";
			int count = stmt.executeUpdate(sql);
			if(count > 0) {
				System.out.println("操作成功!");
			}
			
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception el) {
			el.printStackTrace();
		} finally {
			//关闭数据库
			try {
				if(stmt != null)stmt.close();
				if(connection != null)connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

以上代码我总结一下要注意的点(感觉写注释并不想看诶。。)

1.对于多异常,一定要逐个抛出;

2.由于有一个close无论如何都需要执行,所以需要使用try,catch模式捕获异常

3.注意close中有可能发生的空指针异常。

b.statement的query(查)

只展示和上部分代码不同的部分:

public static void query() {		
		Connection connection = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			//1.导入驱动包,加载具体的驱动类
			Class.forName("oracle.jdbc.OracleDriver");
			//2.和数据库建立链接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3.发送SQL语句(增删改,查)
			stmt = connection.createStatement();
			//4.返回结果
			String sql = "select stuno,stuname,stuage from student2";
			rs = stmt.executeQuery(sql);
			while(rs.next()) {   //resultMap相当于
				int sno = rs.getInt("stuno");
				String sname = rs.getString("stuname");
				System.out.println("sno = "+ sno + "-" + sname);
			}
			
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception el) {
			el.printStackTrace();
		} finally {
			//关闭数据库
			try {
				if(rs != null) rs.close(); //和栈类似,先开后关,后开先关
				if(stmt != null) stmt.close();
				if(connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

1.和简单的增删改的区别是需要一个ResultSet来去接收查询返回的结果;

ResultSet的特点是:

next():光标下移,判断是否有下一条数据;true/false
previous():  true/false
getXxx(字段名|位置):获取具体的字段值 

2.记住保存ResultSet的对象rs也需要关闭,并且同样是先开后关,后开先关的原则。

二、PreparedStatement(这个d...)

public interface PreparedStatement extends Statement 

PreparedStatement 是一个继承statement接口的接口;

String sql = "insert into student2 values(?,?,?,?)";
		    preStmt = connection.prepareStatement(sql);
		    preStmt.setInt(1, 14);
		    preStmt.setString(2, "wb");
		    preStmt.setInt(3, 24);
		    preStmt.setString(4, "g4");
		    
		    int count = preStmt.executeUpdate();
		    if(count > 0) {
				System.out.println("操作成功!");
			}

prepareStatement和Statement区别:

1.首先prepareStatement继承statement方法,但有自己的改进;

2.sql语句:a.sql(可能存在占位符?)在创建PreparedStatement 对象时,将sql预编译

                       prepareStatement(sql)

                       connection.createStatement()

                     b.Statement:executeUpdate(sql)    prepareStatement:executeUpdate(无);

                     总之相当于sql语句前移预编译了。

推荐使用prepareStatement因为连接字符串更简单。

String name = "zs" ;
int age = 23 ;

stmt:
String sql =" insert into student(stuno,stuname) values('"+name+"',  "+age+" )    " ;
stmt.executeUpdate(sql);

pstmt:
String sql =" insert into student(stuno,stuname) values(?,?) " ;
pstmt = connection.prepareStatement(sql);//预编译SQL
pstmt.setString(1,name);
pstmt.setInt(2,age);

传入大文件到数据库:

从数据库读取大文件

发布了103 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zzf_forgot/article/details/88859628
今日推荐