开发环境:
MySQL 5.5.40
IntelliJ IDEA
Navicat Premium 12
JDBCUtils连接数据库
package com.openlab.untils;
import java.sql.*;
public class JDBCUtils {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/oa";
public static final String USERNAME = "root";
public static final String PASSWORD="star123456";
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection()throws SQLException {
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
public void close(Connection connection , Statement pstmt, ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
创建接口impl
UserDaoImpl
package com.openlab.dao.impl;
import com.openlab.bean.Users;
import com.openlab.dao.UsersDao;
import com.openlab.untils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UsersDaoImpl implements UsersDao {
JDBCUtils jdbcUtils = new JDBCUtils();
@Override
public Users findByNameAndPwd(String name, String pwd) {
Connection connection = null;
PreparedStatement pstmt =null;
ResultSet rs = null;
Users users = null;
try {
connection = jdbcUtils.getConnection();
pstmt = connection.prepareStatement("SELECT * FROM users WHERE uname=? AND upwd=?");
pstmt.setString(1,name);
pstmt.setString(2,pwd);
rs = pstmt.executeQuery();
if (rs.next()){
users = new Users();
int uid = rs.getInt("uid");
String uname = rs.getString("uname");
String upwd = rs.getString("upwd");
users.setUid(uid);
users.setUname(uname);
users.setUpwd(upwd);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
jdbcUtils.close(connection,pstmt,rs);
}
return users;
}
}
UserDao
package com.openlab.dao;
import com.openlab.bean.Users;
public interface UsersDao {
public Users findByNameAndPwd(String name, String pwd);
}
主界面Myviews
package com.openlab.views;
import com.openlab.bean.Users;
import com.openlab.dao.UsersDao;
import com.openlab.dao.impl.UsersDaoImpl;
import java.util.Scanner;
public class Myviews {
Scanner input = new Scanner(System.in);
UsersDao usersDao = new UsersDaoImpl();
public void mainmenu(){
System.out.println("1.登录\t2.注册\t0.推出");
System.out.println("请选择");
int choose = input.nextInt();
switch (choose){
case 1:
login();
break;
case 2:
break;
case 0:
System.out.println("谢谢使用");
}
}
public void login(){
System.out.println("请输入用户名");
String name = input.next();
System.out.println("请输入密码");
String pwd = input.next();
Users users = usersDao.findByNameAndPwd(name,pwd);
if (users==null){
System.out.println("登录失败");
mainmenu();
}else {
System.out.println("欢迎您"+users.getUid()+"--"+users.getUname());
}
}
public void menu(){
System.out.println("1.查看学生信息\t2.添加信息\t3.修改信息\t4.删除信息\t0.返回");
int choose = input.nextInt();
switch (choose){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 0:
mainmenu();
break;
}
}
}