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

📄 scorerecord.java

📁 一个简单的C/S模式考试系统
💻 JAVA
字号:
import java.sql.*;
public class ScoreRecord{
        Connection con;
        Statement stm;
        ResultSet res1;
    String    dbname="test";
	String    user="123";
	String    pass="s";
	String    db="sqlserver";
        
          public  ScoreRecord(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  void setScore(String index,String score,String ip,String ui){
          	   
          	   String eq="insert into score(id,score,ip,ui) values('"+index+"','"+score+"','"+ip+"','"+ui+"')";
          	   eq=eq.trim();
          	   int t=1;
          	 
      try {     
                    
       t=stm.executeUpdate(eq);//System.out.print(eq);
         
          
              
           } 
        catch(Exception ex)        { 
         {
       	eq="update score set id='"+index+"',score='"+score+"',ip='"+ip+"',ui='"+ui+"' where id='"+index+"'";
       	try {     
                    
       t=stm.executeUpdate(eq);//System.out.print(eq);
         
          
              
           } 
        catch(Exception ex1)        { System.out.println("最无疑"+ex1.toString()); 
        }  }
        }
        }
    public void stop(){
   	try{
   		
   		con.close();
   	}
   	catch(SQLException error){
   	
   	}
   }   
       
           

   public static void main(String args[]){
    	ScoreRecord p=new ScoreRecord("test","sa","123456","sqlserver");
    	p.loadDatabase();
        p.setScore("13","100","192.168.1.1","郑书成");
    	
    }      

}

⌨️ 快捷键说明

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