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

📄 exec.java

📁 一个简单的C/S模式考试系统
💻 JAVA
字号:
import java.sql.*;
import java.util.Iterator;

public class exec{
        Connection con;
        Statement stm;
        ResultSet res1;
       
        String    dbname="test";
	String    user="123";
	String    pass="s";
	String    db="sqlserver";
	String danan;
        
          public  exec(String dbname,String user,String pass,String db){
          	this.dbname=dbname.trim();
          	this.user=user.trim();
          	this.pass=pass.trim();
          	this.db=db;
          	
          }
  public void loadDatabase(){
    if(this.db=="sqlserver")
      {	try{
    		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    	}
    	catch(ClassNotFoundException err){
    		System.out.println("驱动未找到");
    	}
    	try{
    		con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName="+dbname,user,pass);
    		stm=con.createStatement();
    		
    	}
    	catch(SQLException error){
    		System.out.println("醉了"+error.toString());
    	}
      }	
    if(this.db=="access")
      {	try{
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	}
    	catch(ClassNotFoundException err){
    		System.out.println("驱动未找到");
    	}
    	try{
    		con=DriverManager.getConnection("jdbc:odbc:"+dbname,user,pass);
    		stm=con.createStatement();
    		
    	}
    	catch(SQLException error){
    		System.out.println("醉了"+error.toString());
    	}
       }	      
      if(this.db=="mysql")
      {	try{
    		Class.forName("com.mysql.jdbc.Driver");
    	}
    	catch(ClassNotFoundException err){
    		System.out.println("驱动未找到");
    	}
    	try{
    		con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+dbname+"?user="+user+"&password="+pass);
    		stm=con.createStatement();
    		
    	}
    	catch(SQLException error){
    		System.out.println("醉了"+error.toString());
    	}
       }
       if(this.db=="access数据库直连")
      {	try{
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	}
    	catch(ClassNotFoundException err){
    		System.out.println("驱动未找到");
    	}
    	try{
    		con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbname,user,pass);
    		stm=con.createStatement();
    		
    	}
    	catch(SQLException error){
    		System.out.println("醉了"+error.toString());
    	}
       }	      
        }  
public  String getMax(){
          	   String buff="hao"; 
      try {                
       res1=stm.executeQuery("select count(*) from answer");
    while (res1.next())      {
         buff=res1.getString(1);//读取题数               
              }
              
              return buff;
        }    
        catch(Exception ex)        { System.out.println(ex.toString()); 
        buff=ex.toString();}
          return buff; }
      
public  String getContent(String index){
          	   String buff="";
          	  
          	  int flag3=0;
         RandomTest r=new RandomTest();
        Iterator it1=r.getRandom();
        buff=it1.next().toString();
String eq="select * from answer where index1='"+buff+"'";
        while(it1.hasNext())
        {
            eq=eq+" or index1='"+it1.next().toString()+"'";
        }
          	   
          	   //eq=eq.trim();
          	 
      try {     
      buff="";
      int flag2=1;
            String type=""; 
       String type2=String.valueOf(flag2);  
       String daan1="";          
       res1=stm.executeQuery(eq);//System.out.print(eq);
    while (res1.next())      {
    	type2=String.valueOf(flag2);
       String flag1="NULL";
       type=res1.getString(8).trim();
        
        String timu=res1.getString(2);
        if(type.equals("a"))
       buff=buff+"a@"+type2+"."+timu+"\n";//读取题数
       if(type.equals("b")) 
        buff=buff+"b@"+type2+"."+timu+"\n";
       if(type.equals("c")) 
        buff=buff+"c@"+type2+"."+timu+"\n";
       if((flag1=res1.getString(3))!=null)
       buff=buff+flag1+"\n";             
       if((flag1=res1.getString(4))!=null)
       buff=buff+flag1+"\n";
       if((flag1=res1.getString(5))!=null)
       buff=buff+flag1+"\n";
       if((flag1=res1.getString(6))!=null)
       buff=buff+flag1+"\n";           
       //buff=buff+res1.getString(5)+"\n";    
       //buff=buff+res1.getString(6)+"\n"; 
       daan1=daan1+"@"+res1.getString(7); 
          flag2++;
                              
        
            
        } 
        this.danan=daan1;
        System.out.println(daan1);
        System.out.println(flag2);
        return buff;  } 
        catch(Exception ex)        { System.out.println("最无疑"+ex.toString()); 
       buff=ex.toString(); }
           return buff;
           }
           
     
public  String getAnswer(){
          return this.danan; }
  public void stop(){
   	try{
   		
   		con.close();
   	}
   	catch(SQLException error){
   	
   	}
   }
    public static void main(String args[]){
    	exec p=new exec("test","sa","123456","sqlserver");
    	p.loadDatabase();
    	System.out.print(p.getContent("1"));
    	System.out.print(p.getAnswer());
    	
    }      

}

⌨️ 快捷键说明

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