Java+Swing+sqlServer学生成绩管理系统(DAY 10)连接数据库登录
1.准备工作
-
数据库配置参考教程eclipse连接sqlServer -
jdbc驱动下载 -
建立好数据库(示例数据库可能不规范,因为只作为测试登录与查询) create database StudentGrade
create table manager
(id int
constraint c1 not null
constraint man_UK unique ,
usename varchar(10)
constraint c2 not null,
pwd varchar(20)
constraint c3 not null,
constraint managerPK primary key(id),
)
insert into manager values (1,'陆离','lx0411');
create table student
(id int
constraint c1 not null
constraint UK unique,
sname varchar(10)
constraint c2 not null,
sno varchar(10),
homeTown varchar(10),
cnScore int ,
enScore int ,
mathScore int ,
totalScore int ,
constraint student_PK primary key(sno),
)
insert into student values(1,'张三',
'202001','长沙',97,98,99,294)
2.问题引入
- 连接数据库需要实现的类有哪些
- 如何获取数据库中的表中的信息,与用户输入的登录名和密码信息匹配,实现登录页面验证登录
3.遇到的问题
-
无法打开登录所请求的数据库 “StudentGrade”。登录失败。 win+R——》输入cmd——》在cmd窗口输入sqlcmd——》select name from master.dbo.sysdatabases 查看数据库是否存在,如果不存在说明数据库没有创建 -
创建表的pwd的列属性时,使用的时char类型,验证登录时总会报密码错误,因为用getString()从数据库中获取密码时会自动补齐空格,可以在创建属性时避免用char类型,或者在验证登录时切割掉自动补齐的那部分空格(具体操作详见代码注释中)
2.代码实现
DBUtil
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=StudentGrade";
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String USER="sa";
private static final String PWD="lxloveth0411";
static {
try {
Class.forName(DRIVER);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
try {
return DriverManager.getConnection(URL,USER,PWD);
}catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void closeConn(Connection connection) {
if(connection!=null) {
try {
connection.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closePs(PreparedStatement ps) {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeRs(ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}
AdminDo
package entity;
import java.util.Objects;
public class AdminDO {
private Integer id;
private String usename;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public static void main(String[] args) {
}
}
AdminService
package service;
import entity.AdminDO;
public interface AdminService {
boolean validataAdmin(AdminDO adminDO);
}
AdminServiceImpl
package service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import entity.AdminDO;
import util.DBUtil;
public class AdminServiceImpl implements AdminService{
@Override
public boolean validataAdmin(AdminDO adminDO) {
String sql="select pwd from Manager where usename=?";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn = DBUtil.getConn();
System.out.println("数据库成功");
if(conn==null) {
return false;
}
ps = conn.prepareStatement(sql);
ps.setString(1, adminDO.getUsename());
rs = ps.executeQuery();
while(rs.next()) {
String pwd = rs.getString(1);
pwd = pwd.substring(0,pwd.indexOf(" "));
System.out.println("pwd:"+pwd.length()+":"+pwd);
System.out.println(adminDO.getPwd().length());
if(adminDO.getPwd().equals(pwd)) {
System.out.println("equals");
return true;
}else {
System.out.println("not equals");
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.closeRs(rs);
DBUtil.closeConn(conn);
DBUtil.closePs(ps);
}
return false;
}
}
StudentService(下一节实现)
package service;
public interface StudentService {
}
StudentServiceImpl(下一节实现)
LoginHandler
package handler;
import view.*;
import javax.swing.*;
import entity.AdminDO;
import service.AdminService;
import service.AdminServiceImpl;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginHandler extends KeyAdapter implements ActionListener{
private LoginView loginView;
public LoginHandler(LoginView loginView){
this.loginView=loginView;
}
@Override
public void actionPerformed(ActionEvent e){
JButton jButton=(JButton) e.getSource();
String text=jButton.getText();
if("重置".equals(text)){
loginView.getNameTxd().setText("");
loginView.getPwdFiled().setText("");
}else if("登录".equals(text)){
login();
}
}
private void login() {
String user=loginView.getNameTxd().getText();
char []chars=loginView.getPwdFiled().getPassword();
if(user==null||"".equals(user.trim())||chars==null) {
JOptionPane.showMessageDialog(loginView,"用户名密码必填!");
return ;
}
String pwd=new String(chars);
System.out.println(user+":"+pwd);
AdminService adminService=new AdminServiceImpl();
AdminDO adminDO =new AdminDO();
adminDO.setUsename(user);
adminDO.setPwd(pwd);
boolean flag=adminService.validataAdmin(adminDO);
System.out.println("boolean:"+flag);
if(flag){
new MainView();
loginView.dispose();
}else{
JOptionPane.showMessageDialog(loginView,"用户名密码错误");
}
}
@Override
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_ENTER==e.getKeyCode()) {
login();
}
}
}
ystem.out.println(“boolean:”+flag); if(flag){ //如果登录成功在这里进入主界面并销毁登录界面 new MainView(); loginView.dispose();
}else{
JOptionPane.showMessageDialog(loginView,"用户名密码错误");
}
}
@Override
//键盘监听,按下回车键
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_ENTER==e.getKeyCode()) {
login();
}
}
}
|