📄 database.java
字号:
package dssserver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DataBase {
Connection con;
Statement stmt;
ResultSet rs;
boolean checkuser(Client s){ //检查用户
String password = new String();
try{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("没有找到驱动类:");
System.out.println(e.getMessage());
}
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=userinformation","sa","ccjin");
stmt = con.createStatement();
rs = stmt.executeQuery(" Select * From userinfo Where name='"+s.name+"'");
rs.next();
password = rs.getString(2);
con.close();
}
catch(SQLException ex){
if(ex!=null){
System.out.println("数据库异常被捕获");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
}
}
if(s.pswd.equals(password)){
return(true);
}
else{
return(false);
}
}
boolean checkuser(String loginname,String loginpswd){ //服务器登录
String password = new String();
try{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("没有找到驱动类:");
System.out.println(e.getMessage());
}
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=userinformation","sa","ccjin");
stmt = con.createStatement();
rs = stmt.executeQuery(" Select * From serveruser Where logname='"+loginname+"'");
rs.next();
password = rs.getString(2);
con.close();
}
catch(SQLException ex){
if(ex!=null){
System.out.println("数据库异常被捕获");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
}
}
if(loginpswd.equals(password)){
return(true);
}
else{
return(false);
}
}
void reguser(String name,String pswd){ //用户注册
try{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("没有找到驱动类:");
System.out.println(e.getMessage());
}
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=userinformation","sa","ccjin");
stmt = con.createStatement();
stmt.executeUpdate("Insert Into userinfo Values('"+name+"','"+pswd+"')");
con.close();
}
catch(SQLException ex){
if(ex!=null){
System.out.println("数据库异常被捕获");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -