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

📄 database.java

📁 用java+servlet实现在线考试系统
💻 JAVA
字号:
package com.zte.database;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.zte.beans.ExamQuestion;
import com.zte.beans.ExamSystem;
import com.zte.beans.ExamType;

public class Database {
	
	
	String url;
	Connection con;
	Statement sta;
	ResultSet rs;
	int rows;
	public void getConnection()
	{
		url="jabc:oracle:thin:@localhost:1521:lihao";
		try 
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con=DriverManager.getConnection(url,"lihao","lihao");
			sta=con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ResultSet query(String sql)
	{
		getConnection();
		try {
			rs=sta.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
    
	public boolean update(String sql)
	{
		boolean success=true;
		getConnection();
		try {
			rows=sta.executeUpdate(sql);
			if(rows>0)
			{
				success=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success=false;
		}
		return success;
	}
	
	public boolean add(String sql)
	{
		boolean success=true;
		getConnection();
		try {
			rows=sta.executeUpdate(sql);
			if(rows==1)
			{
				success=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success=false;
		}
		return success;
		
	}
	
	public boolean delete(String sql)
	{
		boolean success=true;
		getConnection();
		try {
			rows=sta.executeUpdate(sql);
			if(rows>0)
			{
				success=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success=false;
		}
		return success;
	}
	
	public List querylist(String sql)
	{
		getConnection();
		List<ExamSystem> list= new ArrayList<ExamSystem>();
		
		try 
		{
			rs=sta.executeQuery(sql);
			while(rs.next())
			{
				ExamSystem es= new ExamSystem();
				es.setStuNumber(rs.getString("stuNumber"));
				es.setStuName(rs.getString("stuName"));
				es.setStu_id(rs.getString("stu_id"));
				es.setAddress(rs.getString("address"));
				es.setClassName(rs.getString("className"));
				es.setStuMail(rs.getString("stuMail"));
				es.setStuPhone(rs.getString("stuPhone"));
				es.setHumanId(rs.getString("humanId"));
				list.add(es);
			}
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	 
	public List questionlist(String sql)
	{
		getConnection();
		List<ExamQuestion> lists=new ArrayList<ExamQuestion>();
		
		try {
			rs=sta.executeQuery(sql);
			while(rs.next())
			{   
				ExamQuestion eq= new ExamQuestion();
				eq.setQu_id(rs.getString("qu_id"));
				eq.setTesttitle(rs.getString("testtitle"));
				eq.setQutitle(rs.getString("qutitle"));
				eq.setQuhard(rs.getString("quhard"));
				eq.setQuscore(rs.getString("quscore"));
				eq.setQutype(rs.getString("qutype"));
				eq.setSelectoption(rs.getString("selectoption"));
				eq.setQuanswer(rs.getString("quanswer"));
				lists.add(eq);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lists;
	}
	public List TypeQuerylist(String sql)
	{
		getConnection();
		List<ExamType> lists=new ArrayList<ExamType>();
		
		try {
			rs=sta.executeQuery(sql);
			while(rs.next())
			{
				ExamType et= new ExamType();
				et.setType_id(rs.getString("type_id"));
				et.setTesttitle(rs.getString("testtitle"));
				et.setTesttime(rs.getString("testtime"));
				lists.add(et);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lists;
	}
	public void close()
	{
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			sta.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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