⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chengjidaoimpl.java

📁 学习 java 做 的一些 小 试验。。。。。。。可以
💻 JAVA
字号:
package com.wczy.daoimpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import java.sql.Connection;
import com.wczy.dao.ChengjiDao;
import com.wczy.common.db.DBConnect;
import com.wczy.pojo.Chengji;

public class ChengjiDaoImpl implements ChengjiDao {

	public boolean delete(Chengji chengji) {
		Connection con = DBConnect.getConnection();
		String sql = "delete from chengji where id=?";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, chengji.id);
			int result = pstmt.executeUpdate();
			return result > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//DBConnect.closeConnection(con);
		}
		return false;
	}

	public boolean isAddOk(Chengji chengji) {
		Connection con = DBConnect.getConnection();
		try {
			String sql = "insert into chengji values (?,?,?,?,?)";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, 1);
			pstmt.setString(2, chengji.name);
			pstmt.setFloat(3, chengji.yuwen);
			pstmt.setFloat(4, chengji.shuxue);
			pstmt.setFloat(5, chengji.yingyu);
			int temp = pstmt.executeUpdate();
			return temp > 0;
		} catch (Exception ee) {
			ee.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//DBConnect.closeConnection(con);
		}
	}

	public ArrayList select(String sql) {
		ArrayList<Chengji> arr = new ArrayList<Chengji>();
		Connection con = DBConnect.getConnection();
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Chengji chengji = new Chengji();
				chengji.setId(rs.getInt(1));
				chengji.setName(rs.getString(2));
				chengji.setYuwen(rs.getFloat(3));
				chengji.setShuxue(rs.getFloat(4));
				chengji.setYingyu(rs.getFloat(5));
				arr.add(chengji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//DBConnect.closeConnection(con);
		}
		return arr;
	}

	public boolean update(Chengji chengji) {
		Connection con = DBConnect.getConnection();
		String sql = "update chengji set name=?,yuwen=?,shuxue=?,yingyu=? where id=?";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, chengji.name);
			pstmt.setFloat(2, chengji.yuwen);
			pstmt.setFloat(3, chengji.shuxue);
			pstmt.setFloat(4, chengji.yingyu);
			pstmt.setInt(5, chengji.id);
			int result = pstmt.executeUpdate();
			return result > 0;
		} catch (Exception eee) {
			eee.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//DBConnect.closeConnection(con);
		}
		return false;
	}

}

⌨️ 快捷键说明

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