score.java

来自「实习的时候做的数字校园系统」· Java 代码 · 共 198 行

JAVA
198
字号
package com.xxgl.business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xxgl.data.DBconnection;
import com.xxgl.data.DBUtil;
import com.xxgl.bean.ScoreBean;

public class Score {

	public int insertScore(ScoreBean Score) {
		String sql = "insert into score (s_id,kch,score) values (?,?,?)";

		Connection con = DBconnection.getConnection();
		PreparedStatement pstmt = null;

		int count = 0;
		try {
			pstmt = con.prepareStatement(sql);

			pstmt.setInt(1, Score.getS_id());
			pstmt.setString(2, Score.getKch());
			pstmt.setInt(3, Score.getScore());

			count = pstmt.executeUpdate();

			DBUtil.close(pstmt);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con);
		}
		return count;
	}

	public int updateScore(ScoreBean score) {
		String sql = "update score set score=? where s_id=? and kch=?";

		Connection con = DBconnection.getConnection();
		PreparedStatement pstmt = null;
		int count = 0;
		try {
			pstmt = con.prepareStatement(sql);

			pstmt.setString(3, score.getKch());
			pstmt.setInt(2, score.getS_id());
			pstmt.setInt(1, score.getScore());

			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(pstmt);
			DBUtil.close(con);
		}

		return count;
	}

	public int deleteBy(int s_id,String kch) {
		String sql = "delete from score where s_id=? and kch=?";

		Connection con = DBconnection.getConnection();
		PreparedStatement pstmt = null;

		int count = 0;
		try {
			pstmt = con.prepareStatement(sql);

			pstmt.setInt(1, s_id);
			pstmt.setString(2, kch);

			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(pstmt);
			DBUtil.close(con);
		}

		return count;
	}

	public ScoreBean selectScore(int s_id,String kch) {
		String sql = "select s_id,kch,score from score where s_id=? and kch=?";

		Connection con = DBconnection.getConnection();
		PreparedStatement pstmt = null;
		ScoreBean pdBean = null;
		try {
			pstmt = con.prepareStatement(sql);

			pstmt.setInt(1, s_id);
			pstmt.setString(2,kch);

			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				pdBean = new ScoreBean();

				pdBean.setS_id(rs.getInt("s_id"));
				pdBean.setKch(rs.getString("kch"));
				pdBean.setScore(rs.getInt("score"));
				
			}
			DBUtil.close(rs);
			

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(pstmt);
			DBUtil.close(con);
		}

		return pdBean;
	}
	
	public List selectAll() {
		String sql = "select s_id,kch,score from score ";

		Connection con = DBconnection.getConnection();
		PreparedStatement pstmt = null;
		List list = new ArrayList();
		
		try {
			pstmt = con.prepareStatement(sql);

			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				ScoreBean pdBean = new ScoreBean();

				pdBean.setS_id(rs.getInt("s_id"));
				pdBean.setKch(rs.getString("kch"));
				pdBean.setScore(rs.getInt("score"));
				
				list.add(pdBean);
			}
			DBUtil.close(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(pstmt);
			DBUtil.close(con);
		}

		return list;
	}

	
//	public static void main(String[] args) {
//		ScoreBean bean = new ScoreBean();
		 
//		bean.setS_id(20);
//		bean.setKch("37");
//      bean.setScore(65);
//		
//		Score bo = new Score();
//		int count = bo.insertScore(bean);
//		System.out.println("count: " + count);
//		System.out.println("score: " + bean.getScore());
		
//		bean.setS_id(20);
//		bean.setKch("37");
//		Score bo = new Score();
//		int count = bo.updateScore(bean);
//		System.out.println("count: " + count);
		
//		bean.setS_id(20);
//		bean.setKch("37");
//		bean.setScore(65);
	
//		Score bo = new Score();
//		int count = bo.deleteBy(20,"37");
//		System.out.println("count=" + count);
		
//		Score bo = new Score();
//		ScoreBean testBean = bo.selectScore(20,"37");
//	    System.out.println(testBean.getS_id()+"   "+testBean.getKch()+"   "+"score=" + testBean.getScore());

//		ParameterBO bo = new ParameterBO();
//		ParameterBean testBean = bo.selectParameterById(1);
//		System.out.println("name=" + testBean.getName());
		 
//		Score bo = new Score();
//		 List list = bo.selectAll();
//		 System.out.println("list length: " + list.size());
//		 for(int i = 0; i < list.size(); i++) {
//			 ScoreBean testBean = (ScoreBean)list.get(i);
//			 System.out.println(testBean.getS_id()+"   "+testBean.getKch()+"   "+testBean.getScore());
//		 }
//	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?