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

📄 test.java

📁 本人大二学习汇编语言程序设计时的全部源代码
💻 JAVA
字号:
package com.paradise.jdbc.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.paradise.jdbc.Student;
import com.paradise.jdbc.util.DB;

public class Test {

	public static void main(String[] args) {
		Student s = new Student();
		Test t = new Test();
		s.setStuno("F0601110109");
		s.setStuname("jack");
		s.setChinese(100);
		s.setEnglish(100);
		s.setMath(100);
		t.insert(s);
		//t.delete(1);
		//t.deleteByStuno(..);
		//t.update(s);
		t.queryAll();
		//t.queryByName(name);
		//t....
	}
	
	public boolean delete(int id) {
		boolean flag = false;
		Connection conn = null;
		String sql = "delete from grade where id = " + id;		
		int result = DB.executeUpdate(conn, sql);
		if(result >= 1) flag = true;  
		DB.closeConn(conn);
		return flag;
	}
	
	public boolean deleteByStuno(String stuno) {
		boolean flag = false;
		Connection conn = null;
		String sql = "delete from grade where stuno = '" + stuno + "'";		
		int result = DB.executeUpdate(conn, sql);
		if(result >= 1) flag = true;  
		DB.closeConn(conn);
		return flag;
	}
	
	public void update(Student s) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "update grade set stuno = ? , stuname = ? , chinese = ?, english = ? , math = ? where id = " + s.getId();		
		try {
			conn = DB.getConn();
			pstmt = DB.getPstmt(conn, sql);
			pstmt.setString(1, s.getStuno());
			pstmt.setString(2, s.getStuname());
			pstmt.setInt(3, s.getChinese());
			pstmt.setInt(4, s.getEnglish());
			pstmt.setInt(5, s.getMath());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closePstmt(pstmt);
			DB.closeConn(conn);
		}
	}
	
	public void insert(Student s) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "insert into grade values (null, ?, ?, ?, ?, ?) ";		
		try {
			conn = DB.getConn();
			pstmt = DB.getPstmt(conn, sql);
			pstmt.setString(1, s.getStuno());
			pstmt.setString(2, s.getStuname());
			pstmt.setInt(3, s.getChinese());
			pstmt.setInt(4, s.getEnglish());
			pstmt.setInt(5, s.getMath());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closePstmt(pstmt);
			DB.closeConn(conn);
		}
	}
	
	public void queryAll() {
		Connection conn = null;
		ResultSet rs = null;
		String sql = "select * from grade";		
		try {
			conn = DB.getConn();
			rs = DB.executeQuery(conn, sql);
			while(rs.next()) {			
				System.out.println(rs.getInt("id") + " " + rs.getString("stuno") + " " + rs.getString("stuname") + " " 
						+ rs.getInt("chinese") + " "+ rs.getInt("english") + " "+ rs.getString("math"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closeRS(rs);
			DB.closeConn(conn);
		}
		
	}
	
	public void queryByName(String name) {
		Connection conn = null;
		ResultSet rs = null;
		String sql = "select * from grade where stuname = '" + name + "'";
		try {
			conn = DB.getConn();
			rs = DB.executeQuery(conn, sql);
			while(rs.next()) {			
				System.out.println(rs.getInt("id") + " " + rs.getString("stuno") + " " + rs.getString("stuname") + " " 
						+ rs.getInt("chinese") + " "+ rs.getInt("english") + " "+ rs.getString("math"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closeRS(rs);
			DB.closeConn(conn);
		}
		
	}
	
	public void queryById(int id) {
		Connection conn = null;
		ResultSet rs = null;
		String sql = "select * from grade where id = " + id;		
		try {
			conn = DB.getConn();
			rs = DB.executeQuery(conn, sql);
			if(rs.next()) {			
				System.out.println(rs.getInt("id") + " " + rs.getString("stuno") + " " + rs.getString("stuname") + " " 
						+ rs.getInt("chinese") + " "+ rs.getInt("english") + " "+ rs.getString("math"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.closeRS(rs);
			DB.closeConn(conn);
		}
		
	}
	
}

⌨️ 快捷键说明

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