Basic operations on the database in Java

Create user login class

Create a user login class to connect to the database to determine whether the user is a legitimate user, and then give the user corresponding prompts. If it is correct, the login is successful, otherwise the login fails.
Insert picture description here
The corresponding code is:

package net.lyq.student.test;

import net.lyq.student.dbutil.ConnectionManager;

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

/**
 * 功能:用户登陆程序
 * 连接数据库,判断程序是否合法用户
 * 然后给予用户相应提示
 *
 */



public class Login {
    public static void main(String[] args){
        //声明部分
        String username;
        String password;
        Scanner sc = new Scanner(System.in);

        System.out.print("输入用户名:");
        username = sc.next();
        System.out.print("输入密码:");
        password = sc.next();

        Connection conn = ConnectionManager.getConnection();
        try {
            String strSQL = "select * from t_user where username = ? and password = ?";
            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            pstmt.setString(1,username);
            pstmt.setString(2,username);

            ResultSet rs = pstmt.executeQuery();

            if(rs.next()){
                System.out.println("恭喜,登陆成功");
            }else{
                System.out.println("遗憾,登陆失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


    }

}

Note: The correct use of SQL statements and the correct use of Chinese and English punctuation! ! !

Create and add record user class

Add user record class, use it to add it to the database table.
Insert picture description here
Show some below 代码片.

package net.lyq.student.test;



import net.lyq.student.bean.User;
import net.lyq.student.dbutil.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.sql.Timestamp;
import java.util.Scanner;

public class AddUser {
    public static void main(String[] args) {
        String username,password,telephone;
        Scanner sc= new Scanner(System.in);

        //输入部分
        System.out.println("用户名:");
        username=sc.next();
        System.out.println("密码:");
        password=sc.next();
        System.out.println("电话:");
        telephone=sc.next();
        //处理部分
        User user=new User();
        //设置实体属性
        user.setUsername(username);
        user.setPassword(password);
        user.setTelephone(telephone);
        user.setRegisterTime(new Timestamp(new Date().getTime()));
        //获取数据库连接
        Connection conn= ConnectionManager.getConnection();
        //定义sql语句
        String strSQL="insert into t_user(username, password, telephone, register_time) values(?,?,?,?)";
        //创建预备对象
        try {
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            pstmt.setString(1,user.getUsername());
            pstmt.setString(2,user.getPassword());
            pstmt.setString(3,user.getTelephone());
            pstmt.setTimestamp(4, (Timestamp) user.getRegisterTime());
            //执行SQL返回添加的jil
            int count=pstmt.executeUpdate();
            if(count>0){
                System.out.println("恭喜用记录添加成功");
            }else{
                System.out.println("遗憾添加失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }


    }
}


When adding user records, you must correctly correspond to the field type in the data table. If you don’t enter the ID field yourself, remember to set it to automatic growth

Create update user class

Insert picture description here
Show some below 内联代码片.

package net.lyq.student.test;

import net.lyq.student.dbutil.ConnectionManager;

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

public class UpdateUser {
    public static void main(String[] args) {
        // 声明部分
        int id;
        String username, password;
        Scanner sc = new Scanner(System.in);

        // 输入部分
        System.out.print("待编辑记录的id:");
        id = sc.nextInt();
        System.out.print("新用户名:");
        username = sc.next();
        System.out.print("新密码:");
        password = sc.next();

        // 处理部分
        // 1. 获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "update t_user set username = ?, password = ? where id = ?";
        try {
            // 3. 创建预备语句
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setInt(3, id);
            // 5. 执行SQL,返回更新成功的记录数
            int count = pstmt.executeUpdate();
            // 6. 判断是否更新成功
            if (count > 0) {
                // 提示用户更新成功
                System.out.println("恭喜,用户记录更新成功!");
            } else {
                // 提示用户更新失败
                System.out.println("遗憾,用户记录更新失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
    }
}

Create and delete user record class

Insert picture description here
Show some below 内联代码片.

package net.lyq.student.test;

import net.lyq.student.bean.User;
import net.lyq.student.dbutil.ConnectionManager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DisplayALLUsers {
    public static void main(String[] args) {
        //1.获取数据库链接
        Connection conn = ConnectionManager.getConnection();
        try {
        //2.定义SQL语句字符串
        String strSQL = "select * from t_user";

        //3.创建语句对象

            Statement stmt  = conn.createStatement();

            //4.执行SQL查询,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);

            //5.遍历结果集,显示表记录
            while (rs.next()){
                //创建用户实体对象
                User user = new User();
                //利用当前记录各个字段值设置实体属性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
                System.out.println(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnectionManager.closeConnection(conn);

        }

    }
}

Guess you like

Origin blog.csdn.net/weixin_46705517/article/details/107119294