用户管理系统控制台版连接数据库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huyande123/article/details/78459693

建User表

CREATE TABLE `user` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(20) DEFAULT NULL,
  `pwd` VARCHAR(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

User对象(javaBean)

public class User {

private int id;
private String name;
private String pwd;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPwd() {
    return pwd;
}
public void setPwd(String pwd) {
    this.pwd = pwd;
}
@Override
public String toString() {
    return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
    }
}

数据库工具类

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* 连接数据的驱动类
* @author YandeHu
*
*/
public class DbUtil {

private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/db_demo";
private static String userName="root";
private static String password="123456";

public static Connection getCon()throws Exception{
    //加载驱动类
    Class.forName(driver);
    //获取DriverManager
    Connection con = DriverManager.getConnection(url, userName, password);
    System.out.println("数据库连接成功");
    return con;
}

public static void close(Connection con)throws Exception{
    if(con!=null){
        con.close();
    }
}

public static void close(PreparedStatement pstmt,Connection con)throws Exception{
    if(pstmt!=null){
        pstmt.close();
    }
    if(con!=null){
        con.close();
    }
}

public static void close(ResultSet rs,PreparedStatement pstmt,Connection con)throws Exception{
    if(rs!=null){
        rs.close();
    }
    if(pstmt!=null){
        pstmt.close();
    }
    if(con!=null){
        con.close();
    }
 }
}

增删改查的方法

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import com.day.DbUtil.DbUtil;//连接数据库工具类的完整路径

public class DbMethod {

public static void showAll()throws Exception{
    //连接数据库
    Connection con=DbUtil.getCon();
    String sql="select * from user";
    //获取SQL预处理对象
    PreparedStatement pstmt = con.prepareStatement(sql);
    //执行SQL语句
    ResultSet rs = pstmt.executeQuery();
    while(rs.next()){
        int id=rs.getInt("id");
        String name=rs.getString("name");
        String pwd=rs.getString("pwd");
        System.out.println(id+" "+name+" "+pwd);
    }
    DbUtil.close(rs, pstmt, con);
}

public static void findUser(int id)throws Exception{
    //连接数据库
    Connection con = DbUtil.getCon();
    //编写SQL语句
    String sql="select * from user where id=?";
    //获取SQL预处理对象
    PreparedStatement pstmt = con.prepareStatement(sql);
    //填坑
    pstmt.setInt(1, id);
    //执行SQL语句
    ResultSet rs = pstmt.executeQuery();
    if(rs.next()){
        String name=rs.getString("name");
        String pwd=rs.getString("pwd");
        System.out.println(name+" "+pwd);
    }
    DbUtil.close(pstmt, con);
}

public static void addUser()throws Exception{
    //获取数据库连接
    Connection con = DbUtil.getCon();
    //编写SQL语句
    String sql="insert into user(id ,name,pwd) values(null,?,?)";
    //获取预处理的SQL对象
    PreparedStatement pstmt = con.prepareStatement(sql);
    //填坑
    System.out.println("请输入新的用户名");
    Scanner sc =new Scanner(System.in);
    String name=sc.nextLine();
    System.out.println("请输入新的用户密码");
    String pwd=sc.nextLine();
    pstmt.setString(1, name);
    pstmt.setString(2, pwd);

    //执行SQL语句
    int rows = pstmt.executeUpdate();
    System.out.println("添加成功"+rows);
    //关闭数据库
    DbUtil.close(pstmt, con);
}
public static void deleteUser(int id )throws Exception{
    //连接数据库
    Connection con = DbUtil.getCon();
    //编写SQL语句
    String sql="delete from user where id=?";
    //获取预处理对象
    PreparedStatement pstmt = con.prepareStatement(sql);
    //填坑
    pstmt.setInt(1, id);
    //执行SQL语句
    int rows = pstmt.executeUpdate();
    System.out.println("成功删除了"+rows+"个");
    //关闭数据库
    DbUtil.close(pstmt, con);
}

public static void updateUser(int id)throws Exception{
    Connection con = DbUtil.getCon();
    String sql="update user set name=? ,pwd=? where id=?";
    System.out.println("输入新的名字");
    Scanner sc=new Scanner(System.in);
    String name=sc.nextLine();
    System.out.println("输入新的密码");
    String pwd=sc.nextLine();

    //获取预处理的SQL语句
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, name);
    pstmt.setString(2, pwd);
    pstmt.setInt(3, id);

    int row = pstmt.executeUpdate();
    System.out.println("更新成功"+row);
    DbUtil.close(pstmt, con);
 }
}

显示页面

import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) throws Exception {
    System.out.println("欢迎来到用户管理系统");
    while(true){
        System.out.println("--------0 显示所有用户信息---------");
        System.out.println("--------1 添加用户信息------------");
        System.out.println("--------2 修改用户信息------------");
        System.out.println("--------3 删除用户信息------------");
        System.out.println("--------4 查找用户信息------------");
        System.out.println("--------5 退出系统---------------");
        System.out.println("选择功能");
        Scanner sc=new Scanner(System.in);
        int choose=sc.nextInt();
        switch(choose){
        case 0:
            DbMethod.showAll();
            break;
        case 1:
            DbMethod.addUser();
            break;
        case 2:{

            System.out.println("要修改对应的Id");
            int id=sc.nextInt();
            DbMethod.updateUser(id);
            break;
        }
        case 3:{

            System.out.println("要删除的Id");
            int id=sc.nextInt();
            DbMethod.deleteUser(id);
            break;
        }
        case 4:{
            System.out.println("要查找的Id");
            int id=sc.nextInt();
            DbMethod.findUser(id);
            break;
        }
        case 5:
            System.out.println("退出系统");
            System.exit(0);
        }
    }
 }
}

猜你喜欢

转载自blog.csdn.net/huyande123/article/details/78459693