JDBC连接数据库增删改查

package com.zx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtil {
    private static String url="jdbc:mysql://localhost:3307/数据库名";
    private static String username="root";
    private static String password="******";
    static {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    }catch (ClassNotFoundException e){
        e.printStackTrace();
    }
    }
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }
}
package com.zx.dao.impl;

import com.zx.beans.User;
import com.zx.dao.UserDao;
import com.zx.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public void addUser(User user) {
        Connection connection=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into t_user1 values (null,?,?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getPhone());
            ps.setString(5,user.getAddress());
            ps.execute();
            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void deleteUser(int id) {
        Connection connection=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql="Delete from t_user1 where id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //如果sql语句中有?表示占位符,需要替换问号
            ps.setInt(1,id);
            ps.execute();
            //关闭连接
            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    @Override
    public void updateUser(User user) {
        Connection connection=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql="Update  t_user1 set username=?,password=? where id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //如果sql语句中有?表示占位符,需要替换问号
//        设置新的数据库信息
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getId());
            ps.execute();
            //关闭连接
            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<User> qetUser() {
        ArrayList<User> users = new ArrayList<User>();

        Connection connection = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql="select * from t_user1 ";
            PreparedStatement ps = connection.prepareStatement(sql);
            //如果sql语句中有?表示占位符,需要替换问号
//将数据添加到数据库中,执行sql
            ResultSet set= ps.executeQuery();
            while (set.next()) {//判断是否有下一个结果
//           System.out.println(set);//查到的是对象
                int id = set.getInt("id");
                String name = set.getString("username");
                String pwd = set.getString("password");
                int age = set.getInt("age");
                String phone = set.getString("phone");
                String address = set.getString("address");
                User user = new User(id, name, pwd, age, phone,address);
                users.add(user);
            }
            connection.close();
            return users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUserById(int id) {
        User user=new User();
        Connection connection = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql="select * from t_user1 where id=? ";
            PreparedStatement ps = connection.prepareStatement(sql);
            //如果sql语句中有?表示占位符,需要替换问号
            ps.setInt(1,id);
//将数据添加到数据库中,执行sql
            ResultSet set= ps.executeQuery();
            while (set.next()) {//判断是否有下一个结果
//           System.out.println(set);//查到的是对象
                int i = set.getInt("id");
                String name = set.getString("username");
                String pwd = set.getString("password");
                int age = set.getInt("age");
                String phone = set.getString("phone");
                String address = set.getString("address");
                user = new User(i, name, pwd, age, phone, address);

            }
            connection.close();
            //返回对象
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean Login(String username, String password) {
        Connection connection = null;
        try{
            connection = JDBCUtil.getConnection();
            String sql = "select  * from t_user1  where username=? and password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //如果sql语句中有?表示占位符,需要替换问号
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet set=ps.executeQuery();
            while ((set.next())){
               return true;
            }
//将数据添加到数据库中,执行sql
            ps.execute();
            //关闭连接
            connection.close();

        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值