package DB;
import java.sql.*;
public class ConnDB {
public Connection DBconn(String ServerName,String DBName,String User,String Pwd){
Connection conn=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://"+ServerName+":1433;DatabaseName="+DBName;
conn=DriverManager.getConnection(url,User,Pwd);
//System.out.print("数据库连接成功!");
return conn;
}
catch(Exception ee)
{
//System.out.print(ee.toString());
ee.printStackTrace();
return conn;
}
}
}
**************************************************
ConnDB DBconn=new ConnDB();
Connection conn=DBconn.DBconn("localhost","javatest","sa","");
if(conn==null){
MessageDialog.openInformation(null, "提示","连接数据库失败" );
return;
}
if(User_name.length()>0 && User_pwd.length()>0){
//Statement updaters=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement updaters;
try {
updaters=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String updatesql="select xname,xpassword,xlevel from xm_member where xname='"+User_name+"' and xpassword='"+User_pwd+"'";
ResultSet rs=updaters.executeQuery(updatesql);
rs.last();
int Iscount=rs.getRow();
//Statement stm=conn.createStatement();
//int Iscount=1;
if(Iscount==1){
MessageDialog.openInformation(null, "提示","登陆成功" );
}else{
MessageDialog.openInformation(null, "提示","登陆失败" );
}
updaters.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
MessageDialog.openInformation(null, "提示","用户名,密码不能为空!" );
}
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
本文介绍了一个Java程序如何连接到SQL Server数据库并执行登录验证的过程。通过使用JDBC驱动,程序能够建立数据库连接,并通过执行SQL查询来验证用户的登录信息。
965

被折叠的 条评论
为什么被折叠?



