📄 exec1.java
字号:
import java.sql.*;
public class exec1{
Connection con;
Statement stm;
ResultSet res1;
String user="sa";
String pass="123456";
String id;
String time;//考试时间
String dbname;
String db="sqlserver";
String eq="select * from user1 where id=";
public exec1(String id,String dbna,String user,String pass,String db){
this.id=id;
this.dbname=dbna;
this.user=user;
this.pass=pass;
this.db=db;}
public String print(){
String buff="null";
if(this.db=="sqlserver")
{try { eq=eq.concat(id);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName="+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="考号:"+res1.getString(1)+"\n";//从第1行开始
buff=buff+"姓名:"+res1.getString(2)+"\n";
buff=buff+"性别:"+res1.getString(3)+"\n";
this.time=res1.getString(4).trim();
buff=buff+"考试时间:"+res1.getString(4).trim()+"分钟\n";
}
con.close();
}
catch(Exception ex) { System.out.println("所担负"+ex.toString());
}
}
if(this.db=="access")
{try { eq="select * from user1 where id='"+this.id+"'";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:"+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="考号:"+res1.getString(1)+"\n";//从第1行开始
buff=buff+"姓名:"+res1.getString(2)+"\n";
buff=buff+"性别:"+res1.getString(3)+"\n";
this.time=res1.getString(4).trim();
buff=buff+"考试时间:"+this.time+"分钟\n";
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
buff=ex.toString();}
}
if(this.db=="mysql")
{try { eq=eq.concat(id);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+dbname+"?user="+user+"&password="+pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="考号:"+res1.getString(1)+"\n";//从第1行开始
buff=buff+"姓名:"+res1.getString(2)+"\n";
buff=buff+"性别:"+res1.getString(3)+"\n";
buff=buff+"考试时间:"+res1.getString(4).trim()+"分钟\n";
this.time=res1.getString(4).trim();
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
buff=ex.toString();}
}
if(this.db=="access数据库直连")
{try { eq="select * from user1 where id='"+this.id+"'";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="考号:"+res1.getString(1)+"\n";//从第1行开始
buff=buff+"姓名:"+res1.getString(2)+"\n";
buff=buff+"性别:"+res1.getString(3)+"\n";
this.time=res1.getString(4).trim();
buff=buff+"考试时间:"+this.time+"分钟\n";
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
buff=ex.toString();}
}
return buff; }
public String getName(){
String buff="null";
if(this.db=="sqlserver")
{try { eq=eq.concat(id);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName="+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="姓名:"+res1.getString(2);
this.time=res1.getString(4).trim();
}
con.close();
}
catch(Exception ex) { System.out.println("真实"+ex.toString());
}
}
if(this.db=="access")
{try { eq="select * from user1 where id='"+this.id+"'";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:"+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="姓名:"+res1.getString(2);
this.time=res1.getString(4).trim();
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
buff=ex.toString();}
}
if(this.db=="mysql")
{try { eq=eq.concat(id);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+dbname+"?user="+user+"&password="+pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="姓名:"+res1.getString(2);
this.time=res1.getString(4).trim();
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
// buff=ex.toString();
}
}
if(this.db=="access数据库直连")
{try { eq="select * from user1 where id='"+this.id+"'";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbname,user,pass);
stm=con.createStatement();
res1=stm.executeQuery(eq);
while (res1.next()) {
buff="姓名:"+res1.getString(2);
this.time=res1.getString(4).trim();
}
con.close();
}
catch(Exception ex) { System.out.println(ex.toString());
buff=ex.toString();}
}
return buff; }
public String getTime(){
return time;}
public void stop(){
try{
con.close();
}
catch(SQLException error){
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -