JDBC增删改查

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

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

    //静态
    static {
        InputStream is = null;
        try {
            is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(is);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    public static void clo(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
    }
}

import org.junit.Test;

import java.sql.*;
import java.util.Scanner;

public class j003wen {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {


        j003wen j003wen = new j003wen();
        j003wen.chaid();
    }


    public void deng() throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户名:");
        String users = scanner.next();
        System.out.println("密码:");
        String password = scanner.next();

        String users_password_sql = "select * from upss where users=? and password=?";
        Connection connection = Jdbcut.getConnection();

        PreparedStatement statement = connection.prepareStatement(users_password_sql);
        statement.setString(1, users);
        statement.setString(2, password);
        ResultSet resultSet = statement.executeQuery();


        if (resultSet.next()) {
            System.out.println("登录成功!");
        } else {
            System.out.println("登录失败!");
        }
        Jdbcut.clo(resultSet, statement, connection);
    }

    public void addtext() throws ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("id:");
        int id = scanner.nextInt();
        System.out.println("name:");
        String name = scanner.next();
        System.out.println("price:");
        int price = scanner.nextInt();
        System.out.println("shu:");
        int shu = scanner.nextInt();

        String addsql = "insert into shang (id,name,price,shu) values (?,?,?,?)";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement statement = connection.prepareStatement(addsql);
        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setInt(3, price);
        statement.setInt(4, shu);
        statement.executeUpdate();
        JDBCUtils.clo(null, statement, connection);
    }

    public static void shan() throws SQLException, ClassNotFoundException {

 Scanner scanner = new Scanner(System.in);
        System.out.println("id:");
        int id = scanner.nextInt();
        String shansql = "delete from shang where id = ?";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement statement = connection.prepareStatement(shansql);
        statement.setInt(1, id);
        statement.executeUpdate();
        JDBCUtils.clo(null, statement, connection);
    }

    public static void gai() throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("name:");
        String name = scanner.next();
        System.out.println("price:");
        int price = scanner.nextInt();
        System.out.println("shu:");
        int shu = scanner.nextInt();
        System.out.println("id:");
        int id = scanner.nextInt();

        String gaisql = "update shang set name = ? ,price = ?, shu = ? where id = ? ";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement statement = connection.prepareStatement(gaisql);
        statement.setString(1, name);
        statement.setInt(2, price);
        statement.setInt(3, shu);
        statement.setInt(4, id);
        statement.executeUpdate();
        JDBCUtils.clo(null, statement, connection);
    }

    public static void cha() throws SQLException, ClassNotFoundException {
        String gaisql = "select * from shang";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement statement = connection.prepareStatement(gaisql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int price = resultSet.getInt("price");
            int shu = resultSet.getInt("shu");
            System.out.println(id + ":" + name + ":" + price + ":" + shu);
        }
        JDBCUtils.clo(resultSet, statement, connection);
    }

    public static void chaid() throws SQL

Exception, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("id:");
        int id = scanner.nextInt();

        String gaisql = "select * from shang where id = " + id;
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement statement = connection.prepareStatement(gaisql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            int id1 = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int price = resultSet.getInt("price");
            int shu = resultSet.getInt("shu");
            System.out.println(id1 + ":" + name + ":" + price + ":" + shu);
        }
        JDBCUtils.clo(resultSet, statement, connection);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值