📄 4.24.txt
字号:
一.用java语句通过jdbc操作数据库,实现数据库的增,删,改,查
conn 文件夹
package conn;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConn {
public static Connection getDBConn()
{
Connection conn=null;
try {
/*载入驱动程序*/
Class.forName("oracle.jdbc.driver.OracleDriver");
/*用DriverManager.getConnection()方法连接数据库,有三个参数*/
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbl","scott","tiger");
} catch (Exception e) {
// TODO 自动生成 catch 块
System.out.println("数据库连接失败");
e.printStackTrace();
}
return conn;
}
}
------------------------------------------------------------
vo 文件夹
package vo;
public class StudentVo {
private int id;
private String name;
private int age;
private String depart;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------------------------------------------
dao 文件夹
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import vo.StudentVo;
import conn.DBConn;
public class StudentDao {
***************
/*查询的是一条数据,用StudentVo就可以*/
public StudentVo selectById(int i)
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
StudentVo sto=null;
String sql="select studentid,name,age,department from student where studentid="+i+" ";
//System.out.println(sql);
try {
/*调用类DBConn中的方法,连接数据库*/
conn=DBConn.getDBConn();
/*创建一个 Statement 对象来将 SQL 语句发送到数据库。*/
st=conn.createStatement();
/* 执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。*/
rs=st.executeQuery(sql);
/*因为通过主键来查询数据,数据是唯一的,只执行一次,所以用if语句*/
if(rs.next()) //next()方法是取出下一个的值
{
sto=new StudentVo();
sto.setId(rs.getInt("studentid"));
sto.setName(rs.getString("name"));
sto.setAge(rs.getInt("age"));
sto.setDepart(rs.getString("department"));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
System.out.println("查询失败");
e.printStackTrace();
}finally{
/*最后不要忘了关闭资源和数据库*/
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return sto;
}
********************
/*查询的可能是多条数据,用List作为返回值*/
public List selectByDept(String dept)
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
List l1=new ArrayList();
String sql="select studentid,name,age,department from student where department= '"
+dept+"'";
try {
conn=DBConn.getDBConn();
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
StudentVo svo=new StudentVo();
svo.setId(rs.getInt("studentid"));
svo.setName(rs.getString("name"));
svo.setAge(rs.getInt("age"));
svo.setDepart(rs.getString("department"));
l1.add(svo);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
System.out.println("查询失败");
e.printStackTrace();
}finally{
try {if(rs!=null){
rs.close();
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
if(st!=null){st.close();};
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
if(true){conn.close();}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return l1;
}
**************************************
/*此方法的返回值是整型的,通过返回的值来判断删除数据是否成功*/
public int DlelteDemo(int i)
{
Connection conn=null;
Statement st=null;
int flg=0;
String sql="delete student where studentid="+i+" ";
System.out.println(sql);
try {
conn=DBConn.getDBConn();
/*将提交设置成手动提交,如果此处设成自动,在删除数据的时候,可能出现异常,造成有的数据没被删掉*/
conn.setAutoCommit(false);
/*创建一个 Statement 对象来将 SQL 语句发送到数据库。*/
st=conn.createStatement();
/*执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。*/
flg=st.executeUpdate(sql);
/*手动提交以上的操作*/
conn.commit();
} catch (SQLException e) {
try {
/*出现异常时,恢复成操作前的样子*/
conn.rollback();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
// TODO 自动生成 catch 块
System.out.println("删除失败");
e.printStackTrace();
}finally{
try {
/*最后不要忘记设成自动提交的模式和关闭资源的数据库*/
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return flg;
}
****************************************
/*此方法的返回值是整型的,通过返回的值来判断插入数据是否成功*/
public int InsertDemo(StudentVo s)
{
Connection conn=null;
PreparedStatement pst=null;
int flg=0;
String sql="insert into student (studentid,name,age,department)"+
"values(?,?,?,?)";
try {
conn=DBConn.getDBConn();
conn.setAutoCommit(false);
/* 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。*/
pst=conn.prepareStatement(sql);
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setInt(3, s.getAge());
pst.setString(4, s.getDepart());
flg=pst.executeUpdate();
conn.commit();
} catch (SQLException e) {
// TODO 自动生成 catch 块
try {
conn.rollback();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
System.out.println("插入失败");
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
pst.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return flg;
}
**********************************************
public int UpdateDemo(int i,StudentVo s)
{
Connection conn=null;
PreparedStatement pst=null;
int flg=0;
String sql="update student set studentid=?,name=?,age=?,department=? "+
"where studentid="+i+" ";
try {
conn=DBConn.getDBConn();
conn.setAutoCommit(false);
//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
pst=conn.prepareStatement(sql);
/*将指定参数设置为给定 Java int 值。在将该对象发送到数据库时,驱动程序将它转换成一个 SQL INTEGER 值。 */
pst.setInt(1, s.getId());
pst.setString(2, s.getName());
pst.setInt(3, s.getAge());
pst.setString(4, s.getDepart());
flg=pst.executeUpdate();
conn.commit();
} catch (SQLException e) {
// TODO 自动生成 catch 块
try {
conn.rollback();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
System.out.println("更新失败");
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(pst!=null){try {
pst.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}}
if(conn!=null){try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}}
}
return flg;
}
}
-------------------------------------------------------------------
bo 文件夹
package bo;
import java.util.List;
import vo.StudentVo;
import dao.StudentDao;
public class Business {
/*测试查询一条数据*/
public void displaySelectById(int i)
{
StudentDao sdao=new StudentDao();
StudentVo svo=sdao.selectById(i);
if (svo!=null)
{
System.out.println(svo.getId()+" "+svo.getName()+" "+svo.getAge()+" "+svo.getDepart());
}
}
/*测试查询多条数据*/
public void displaySelectByDept(String dept)
{
StudentDao sdo=new StudentDao();
List l1=sdo.selectByDept(dept);
if(l1!=null)
{
for(int i=0;i<l1.size();i++)
{
StudentVo svo=(StudentVo)l1.get(i);
if(svo!=null){
System.out.println(svo.getId()+" "+svo.getName()+" "+svo.getAge()+" "+svo.getDepart());
}
}
}
}
/*测试删除数据*/
public void displayDelete(int i)
{
StudentDao sdo=new StudentDao();
int flg=sdo.DlelteDemo(i);
if(flg!=0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
/*测试插入数据*/
public void displayInsert(StudentVo s)
{
StudentDao sdo=new StudentDao();
int flg=sdo.InsertDemo(s);
if (flg!=0){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
}
/*测试更新一条数据*/
public void displayUpdate(int i,StudentVo s)
{
StudentDao sdo=new StudentDao();
int flg=sdo.UpdateDemo(i, s);
if(flg!=0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
}
}
------------------------------------------
test 文件夹
package test;
import vo.StudentVo;
import bo.Business;
public class Test {
public static void main(String [] args)
{
Business b1=new Business();
b1.displaySelectById(1);
//b1.displaySelectByDept("计算机");
//b1.displayDelete(6);
StudentVo svo=new StudentVo();
svo.setId(3);
svo.setName("小传");
svo.setAge(24);
svo.setDepart("英语");
//b1.displayInsert(svo);
//b1.displayUpdate(3, svo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -