jdbc的增删改查

连接数据库Tool类

package com.yun.user.tool;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class JdbcUtil {
        private static String url="jdbc:mysql://localhost:3306/mvc";
        private  static String user="root";
        private  static String password="root";
        private static Statement stmt = null;
        private static Connection conn = null;
        private static ResultSet rs;
        public static Connection getConnection() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn=(Connection) DriverManager.getConnection(url, user, password);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return conn;
        }

        public static void closeConnection(Statement stmt,Connection conn){
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        public static void closeConnection(Statement stmt,Connection conn,ResultSet rs){
            if(stmt!=null&&conn!=null){
                closeConnection(stmt,conn);
            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

}

查询

public void test1(){
        Connection conn = null;
        Statement stmt = null;
        try{
            //获取连接
            conn = JdbcUtil.getConnection();
            //创建Statement
            stmt = conn.createStatement();
            //准备sql
            String sql = "SELECT * FROM student";
            //执行sql
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id+","+name+","+gender);
            }

        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            JdbcUtil.close(conn, stmt);
        }

}

增加(用PreparedStatement 的话插入参数是从1开始的)

private Connection conn;
        private PreparedStatement stam;
        public   void add(User user){
            conn=JdbcUtil.getConnection();
            try {
                String sql="insert into user(name,age,birthday) values(?,?,?)";
                stam = (PreparedStatement) conn.prepareStatement(sql);
                stam.setString(1, user.getUsername());
                stam.setInt(2, user.getAge());
                stam.setDate(3, new Date(user.getHiredate().getTime()));
                int line = stam.executeUpdate();
                System.out.println(user.getHiredate());
                System.out.println("影响了"+line+"行");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                JdbcUtil.closeConnection(stam, conn);
            }
}

删除

public void delete(int id){
                conn=JdbcUtil.getConnection();
                String sql="delete from user where id= ?";
                try {
                    stam = (PreparedStatement) conn.prepareStatement(sql);
                    stam.setInt(1, id);
                    int line = stam.executeUpdate();
                    System.out.println("影响了"+line+"行");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtil.closeConnection(stam, conn);
                }
}

通过id查找

public User findUserById(int id){
                conn=JdbcUtil.getConnection();
                String sql="select * from user  where id= ?";
                try {
                    stam = (PreparedStatement) conn.prepareStatement(sql);
                    stam.setInt(1, id);
                    rs = stam.executeQuery();
                    while(rs.next()){
                        String name=rs.getString("name");
                        int age=rs.getInt("age");                   
                        java.util.Date date =new java.util.Date(rs.getDate("birthday").getTime());
                        user =new User(id,name,age,date);
                        System.out.println(user);
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtil.closeConnection(stam, conn,rs);
                }

                return user;
}

//更新

public void update(User user){
                conn=JdbcUtil.getConnection();
                String sql="update user set name=?,age=? ,birthday=? where id=? ";
                try {
                    stam = (PreparedStatement) conn.prepareStatement(sql);
                    stam.setString(1, user.getUsername());
                    stam.setInt(2, user.getAge());
                    stam.setDate(3, new Date(user.getHiredate().getTime()));
                    stam.setInt(4, user.getId());
                    int line = stam.executeUpdate();
                    System.out.println("影响了"+line+"行");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtil.closeConnection(stam, conn);
                }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发疯的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值