JAVA学习日记:JDBC(2)

本节课我学习的主要内容有:

1.Connection接口
2.Statement接口

Connection接口:

		Connection类型的接口对象表示一个数据库连接。
  		
  		基本方法:
  			setAutoCommit() 设置事务是否自动提交。默认自动提交。
 			commit() 提交事务。
  			rollback() 回滚事务。
  			getMetaData() 得到DatabaseMetaData实例,及其数据库的元数据。
 
 			creatStatement() 创建Statement的实例。
  			preparedStatement() 创建PreparedStatementd的实例预编译的Statement。
  			perpareCall() 创建CallableStatement的实例一般用来存储执行过程的。
  
  			close() 关闭与数据库的连接。

Statement接口:

		Statement接口提供了用于指向sql语句并且返回执行结果的方法。
  
  		方法:
  			execute() 执行sql、返回布尔型结果(不常用)。
  			executeUpdate() 执行sql(增删改语句啦之类的dml语句)、返回的是int类型的结果,这个结果是你sql语句执行之后表中被影响的记录行数啦之类的(常用)。
 			executeQuery() 执行查询语句,返回的是ResultSet型的结果(常用)。
  			addBatch()	将sql增加添加到批处理中(常用)。
  			executeBatch() 执行批处理,返回一个int型的数组。
  			executeLargeUpdate() 默认方法、用于执行sql、会影响很多记录行数。

  			close() 关闭Statement对象,释放资源。

OracleConnectionClass(用于连接Oracle数据库):

package LessonForJDBC02;

import java.sql.*;

public class OracleConnectionClass 
{
	public static Connection getConnection(String driver_class,String url,String user,String password)
	{
		Connection con = null;
		
		try 
		{
			Class.forName(driver_class);
			con = DriverManager.getConnection(url, user, password);
			
		} catch (ClassNotFoundException | SQLException e) 
			{
				e.printStackTrace();
			}
		
		return con;
	}
	
	public static void closeConnection(Connection con)
	{
		if (con == null)
		{
			try 
			{
				con.close();
			} catch (SQLException e) 
				{
					e.printStackTrace();
				}
		}
	}
}

Test01(测试Statment类以及对数据库的增删改和简易批处理):

package LessonForJDBC02;

import java.sql.*;

//将要传入数据库的数据封装成一个StudentBean对象。
class StudentBean
{
	private int No;
	private String Name;
	private String Sex;
	private int Age;
	
	public StudentBean() 
	{
		super();
	}
	
	public StudentBean(Integer no, String name, String sex, Integer sage) 
	{
		super();
		No = no;
		Name = name;
		Sex = sex;
		Age = sage;
	}

	public Integer getNo() 
	{
		return No;
	}

	public void setNo(Integer no) 
	{
		No = no;
	}

	public String getName() 
	{
		return Name;
	}

	public void setName(String name) 
	{
		Name = name;
	}

	public String getSex() 
	{
		return Sex;
	}

	public void setSex(String sex) 
	{
		Sex = sex;
	}

	public Integer getAge() 
	{
		return Age;
	}

	public void setAge(Integer Age) 
	{
		Age = Age;
	}
}

class StudentDAO//操作Student的
{
	//增加学生
	public static int addStudent(Connection con)
	{
		
		int result = 0;
		
		String sql = "insert into StudentTest01 (No,Name,Sex,Sage) values (0002,'刘泽民','男',20)";
		
		try 
		{
			Statement sta = con.createStatement();
			
			result = sta.executeUpdate(sql);
			
		} catch (SQLException e) 
			{
				e.printStackTrace();
			}
		
		return result;
	} 
	
	//更新学生信息
	public static int updateInf(Connection con)
	{
		int result = 0;
		
		String sql = "update StudentTest01 set Name = '黄力文'  where No = 0002";
		
		try 
		{
			Statement sta = con.createStatement();
			result = sta.executeUpdate(sql);
		} catch (SQLException e) 
			{
				e.printStackTrace();
			}
		
		return result;
	}
	
	//删除学生信息
	public static int deleteInf(Connection con)
	{
		int result = 0;
		
		String sql = "delete StudentTest01 where No = 1";
		
		try 
		{
			Statement sta = con.createStatement();
			result = sta.executeUpdate(sql);
		} catch (SQLException e) 
			{
				e.printStackTrace();
			}
		
		return result;
	}
	
	//同时添加多名学生(批处理)
	public static int[] batchStudent(Connection con)
	{
		int[] result = null;
		
		String sql1 = "insert into StudentTest01 (NO,NAME,SEX,SAGE) values (0002,'赵力民','男',20)";
		String sql2 = "insert into StudentTest01 (NO,NAME,SEX,SAGE) values (0003,'张一海','男',19)";
		String sql3 = "insert into StudentTest01 (NO,NAME,SEX,SAGE) values (0004,'尚蕊','男','20')";
		String sql4 = "insert into StudentTest01 (NO,NAME,SEX,SAGE) values (0005,'罗天文','男',20)";
		
		try 
		{
			Statement sta = con.createStatement();
			
			sta.addBatch(sql1);
			sta.addBatch(sql2);
			sta.addBatch(sql3);
			sta.addBatch(sql4);
			
			result = sta.executeBatch();
		} catch (SQLException e) 
			{
				e.printStackTrace();
			}
		
		return result;
	}
}

public class Test01 
{
	public static void main(String[] args) 
	{
		Connection con1 = OracleConnectionClass.getConnection("oracle.jdbc.OracleDriver", 
				"jdbc:oracle:thin:@localhost:1521:orcl"	, "scott", "******");
		
		//以下测试均通过
//		System.out.println("此时添加是否成功:"+StudentDAO.addStudent(con1));
//		System.out.println("此时是否修改成功:"+StudentDAO.updateInf(con1));
//		System.out.println("此时是否删除成功:"+StudentDAO.deleteInf(con1));
//		System.out.println("此时批处理是否成功:"+StudentDAO.batchStudent(con1).length);
		
		OracleConnectionClass.closeConnection(con1);
	}
}

本篇部分文字来源于:
咕嘟咖啡杨海滨老师 — 《java编程语言高级特性》
在这里十分感谢老师能够给我带来学习的激情。

2020.11.16
本文章是本人学习笔记,不进行任何商用所以不支持转载请理解!也请别拿去商用!
如果觉得对你有帮助那么欢迎你随时来回顾!
只为记录本人学习历程。
毕
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值