××××××××××××××××××××××××××××××××××××××××××××
import java.sql.*;
import java.util.*;
class DBconnection{
private String sample = "Start value";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
Connection con;
Statement sta;
ResultSet rs;
private String driver;
private String url;
private String user;
private String pwd;
public DBconnection(){/*初始化DBconnectionBean,进行数据库链接*/
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SelectSubjectData";
user="admin";
pwd="admin";
init();
}
public void init(){/*重置 init()方法*/
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getCon(){/* getCon() 返回Connection 对象*/
return(con);
}
public ResultSet executeQuery(String SQL){/* 执行SQL语句*/
rs=null;
try{
sta=con.createStatement();
rs=sta.executeQuery(SQL);
}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());
System.err.println("aq.executeQuerystrSQL:"+SQL);
}
return rs;
}
public void executeUpdate(String SQL){/* 执行SQL语句*/
try{
sta=con.createStatement();
sta.executeUpdate(SQL);
}
catch(SQLException ex){
System.err.println("aq.executeUpdate:"+ex.getMessage());
System.err.println("aq.executeUpdatestrSQL:"+SQL);
}
}
public void destroy(){/* 关闭数据库链接*/
try{
sta.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class Pass {
private String Tnum;
private String Snum;
private String Password;
int auth=0;//密码正确,auth=1;
public void setPass(String pTnum,String pSnum,String pPassword){/* 通过参数传递,查询用户是否存在,密码是否正确*/
PreparedStatement prepare;/* 创建 PreparedStatement 实例*/
ResultSet rs;/* 创建记录集实例 */
String SQL;/* 要执行的 SQL 语句 */
DBconnection dbc=new DBconnection();/* 创建 DBconnection 实例,用于数据库链接 */
Connection con=dbc.getCon();/* 建立数据库链接 */
if(pTnum!="")/* 如果为老师登录,查询 Teacher 表 */
{
try
{
SQL = "select * from Teacher where Tnum=?";
prepare = con.prepareStatement(SQL);
prepare.setString(1, pTnum);
rs=prepare.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
}
else/* 如果为学生,查询 Student 表 */
{
try
{
SQL = "select * from Student where Snum=?";
prepare = con.prepareStatement(SQL);
prepare.setString(1, pSnum);
rs=prepare.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
if(rs.next()) /* 查到用户后,验证密码 */
{
if (pPassword.equals(rs.getString("Password")))
{
auth = 1; /* 密码正确,则 auth=1,成功登录 */
}
else
{
/* 密码错误*/
auth = 2; /*密码错误,则 auth=2 */
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public int getPass()/* 密码是否正确 */
{
return(auth);
}
}×××××××××××××××××××××××××××××××××××××××××××
哪位师兄帮帮忙,忙我编译一下。我真的不知道到底是哪出错了,急呀!求求好心人了!
import java.sql.*;
import java.util.*;
class DBconnection{
private String sample = "Start value";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
Connection con;
Statement sta;
ResultSet rs;
private String driver;
private String url;
private String user;
private String pwd;
public DBconnection(){/*初始化DBconnectionBean,进行数据库链接*/
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SelectSubjectData";
user="admin";
pwd="admin";
init();
}
public void init(){/*重置 init()方法*/
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getCon(){/* getCon() 返回Connection 对象*/
return(con);
}
public ResultSet executeQuery(String SQL){/* 执行SQL语句*/
rs=null;
try{
sta=con.createStatement();
rs=sta.executeQuery(SQL);
}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());
System.err.println("aq.executeQuerystrSQL:"+SQL);
}
return rs;
}
public void executeUpdate(String SQL){/* 执行SQL语句*/
try{
sta=con.createStatement();
sta.executeUpdate(SQL);
}
catch(SQLException ex){
System.err.println("aq.executeUpdate:"+ex.getMessage());
System.err.println("aq.executeUpdatestrSQL:"+SQL);
}
}
public void destroy(){/* 关闭数据库链接*/
try{
sta.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class Pass {
private String Tnum;
private String Snum;
private String Password;
int auth=0;//密码正确,auth=1;
public void setPass(String pTnum,String pSnum,String pPassword){/* 通过参数传递,查询用户是否存在,密码是否正确*/
PreparedStatement prepare;/* 创建 PreparedStatement 实例*/
ResultSet rs;/* 创建记录集实例 */
String SQL;/* 要执行的 SQL 语句 */
DBconnection dbc=new DBconnection();/* 创建 DBconnection 实例,用于数据库链接 */
Connection con=dbc.getCon();/* 建立数据库链接 */
if(pTnum!="")/* 如果为老师登录,查询 Teacher 表 */
{
try
{
SQL = "select * from Teacher where Tnum=?";
prepare = con.prepareStatement(SQL);
prepare.setString(1, pTnum);
rs=prepare.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
}
else/* 如果为学生,查询 Student 表 */
{
try
{
SQL = "select * from Student where Snum=?";
prepare = con.prepareStatement(SQL);
prepare.setString(1, pSnum);
rs=prepare.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
if(rs.next()) /* 查到用户后,验证密码 */
{
if (pPassword.equals(rs.getString("Password")))
{
auth = 1; /* 密码正确,则 auth=1,成功登录 */
}
else
{
/* 密码错误*/
auth = 2; /*密码错误,则 auth=2 */
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public int getPass()/* 密码是否正确 */
{
return(auth);
}
}×××××××××××××××××××××××××××××××××××××××××××
哪位师兄帮帮忙,忙我编译一下。我真的不知道到底是哪出错了,急呀!求求好心人了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货