学校作业——后台通过JDBC连接操作数据库

这篇博客介绍了如何在Java编程环境中,通过JDBC连接工具类实现与MySQL数据库的连接,进而进行数据库的增删改查操作。内容涵盖连接数据库的基本步骤和相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求,在编译器中通过JDBC驱动连接数据库并对其进行增删改查

连接工具类

package com.aroad.webdemo.DJBCtest;

import java.io.FileReader;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static {
        try {
            Properties properties = new Properties();
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            System.out.println(path);

            properties.load(new FileReader(path));

            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");

            Class.forName(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getCounection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) throws SQLException{  //ResultSet 结果集
        if(rs != null){
            rs.close();
            stmt.close();
        }
        if (stmt != null){
            stmt.close();
        }
        if(conn != null){
            conn.close();
        }
    }
}

增删改查操作类

package com.aroad.webdemo.DJBCtest;

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

public class JdbcTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
    }

    public boolean register(String username,String password) throws SQLException{
        if (username == null || password == null)
            return false;

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getCounection();

            String sql = "SELECT * FROM user WHERE username = '"+ username+"'";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                return false;
            }

            sql = "INSERT INTO user (username,password,authrity) values ('"+username+"','"+password+",'+'5')";
            statement = connection.createStatement();
            if (statement.executeUpdate(sql) >= 1)
                return true;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,statement,connection);
            JDBCUtils.close(resultSet,statement,connection);
        }
        return false;
    }

    public boolean login(String username,String password) throws SQLException{
        if (username == null || password == null)
            return false;

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getCounection();

            String sql = "SELECT * FROM user WHERE username = '"+ username+"' AND password = '"+password+"'";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return resultSet.next();
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,statement,connection);
            JDBCUtils.close(resultSet,statement,connection);
        }
        return false;
    }

    public boolean changePwd(String username,String oldPassword,String newPassword) throws SQLException{
        if (username == null || oldPassword == null || newPassword == null)
            return false;


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getCounection();

            String sql = "SELECT * FROM user WHERE username = '"+ username+"' AND password = '"+oldPassword+"'";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                sql = "UPDATE user SET password = '"+newPassword+"' WHERE username = '"+username+"'";
                resultSet = statement.executeQuery(sql);
                return resultSet.next();
            }
            return false;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,statement,connection);
            JDBCUtils.close(resultSet,statement,connection);
        }
        return false;
    }

    public boolean deleteUser(String username) throws SQLException{
        if (username == null)
            return false;


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getCounection();

            String sql = "DELETE FROM user WHERE usaername = '"+username+"'";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,statement,connection);
            JDBCUtils.close(resultSet,statement,connection);
        }
        return false;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值