jdbc:增删改查

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.db.DBConn;
import com.pojo.Student;

/*
 * 数据库数据操作类,进行数据库数据的增删改查(CRUD)
 */
/*
   1>	获取连接
		调用共通类:DbConn.getConn();
   2>	编写sql
		Insert、update、delete、select,(如果sql语句中有参数,使用占位符?表示)
   3>	给占位符赋值
		使用PreparedStatement的一系列的setXXX方法,依次给每个占位符赋值。
   4>	发送执行sql
		【更新系】:包括insert、update、delete三种sql。
		pstmt.executeUpdate()方法执行,返回影响表中的行数(int型)
                          【检索系】select。 
		使用pstmt.executeQuery()方法执行,返回ResultSet结果集
   5>	处理结果
		【更新系】判断结果是否大于0,影响的行数大于0表示sql执行成功。
		【检索系】:从ResultSet结果集中提取数据,放到ArrayList中。
 */
public class StudentDao {
	/*
	 * Student表:增加数据
	 */
	public boolean addStu(Student stu){
		boolean flagAdd = false;
		//1.连接数据
		Connection conn=DBConn.getConn();
		//2.编写sql
		String sql="insert into student values (?,?,?,?,?)";
		//3.给占位符赋值
			//(1)执行静态SQL语句。通常通过Statement实例实现。
		         //Statement stmt=conn.createStatement();
		    //(2)执行动态SQL语句。通常通过PreparedStatement()实例实现。(连接jdbc时通常使用动态语句)
				 //PreparedStatement pstmt=conn.prepareStatement(sql); 
		  	//(3)执行数据库存储过程。通常通过CallableStatement实例实现。
				 //CallableStatement cstmt=conn.prepareCall();
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,stu.getSno());
			pstmt.setString(2,stu.getSname());
			pstmt.setString(3,stu.getSsex());
			pstmt.setInt(4,stu.getSage());
			pstmt.setString(5,stu.getSdept());
			// 4.发送执行sql
			int result = pstmt.executeUpdate(); // 用于执行更新系SQL语句:insert、delete、update,返回整型表示执行sql后影响表中的行数
			if(result>0)
				flagAdd=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//5.关闭数据库连接
			try {
				if(pstmt!=null){				
					pstmt.close();
				} 
				if(conn!=null){
					conn.close();
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flagAdd;
		
	}
	
	/*
	 * 删除数据
	 */
	public boolean deleteStu(String sno){
		boolean flagDel=false;
		Connection conn=DBConn.getConn();
		String sql="delete from student where sno=?";
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,sno);
			int result=pstmt.executeUpdate();
			if(result>0)
				flagDel=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return flagDel;		
	}
	/*
	 * 修改数据
	 */
	public boolean reviseStu(Student stu){
		
		boolean flagRev=false;
		Connection conn=DBConn.getConn();
		String sql="update student set sname=?,ssex=?,sage=?,sdept=? where sno=?";
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			
			pstmt.setString(1,stu.getSname());
			pstmt.setString(2,stu.getSsex());
			pstmt.setInt(3,stu.getSage());
			pstmt.setString(4,stu.getSdept());
			pstmt.setString(5,stu.getSno());
			
			int result=pstmt.executeUpdate();
			if(result>0)
				flagRev=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}	
		return flagRev;
		
	}
	
	/*
	 * 查询数据
	 */
	public List<Student> selectStu(){
		
		List<Student> listStu=new ArrayList<Student>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="select sno stu_sno,sname stu_sname,ssex stu_ssex,sage stu_sage,sdept stu_sdept from student";
		try {
			conn=DBConn.getConn();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Student student=new Student();
				student.setSno(rs.getString("stu_sno"));
				student.setSname(rs.getString("stu_sname"));
				student.setSsex(rs.getString("stu_ssex"));
				student.setSage(Integer.parseInt(rs.getString("stu_sage")));
				student.setSdept(rs.getString("stu_sdept"));
				listStu.add(student);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			try {
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
		return listStu;
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值