栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC PreparedStatement接口完成增删改

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC PreparedStatement接口完成增删改

package JDBC;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBC_prestatement {
    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement ps = null;
        //同步替换变量名shift+F6
        ResultSet rs = null;
        //资源绑定器
        ResourceBundle bundle = ResourceBundle.getBundle("resourses/db");
        String driver= bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");


        try{
            //1.注册驱动的第二种方式:类加载注册
//            Class.forName("com.mysql.jdbc.Driver");//可以写到配置文件当中
            Class.forName(driver);
            //2.获取连接
//            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/trade_for_book","root","002415");
            conn = DriverManager.getConnection(url,user,password);
            //3.获取数据库操作对象
//            实现插入
//            String sql = "insert into t_user(login_name,login_pwd,real_name) values (?,?,?)";
//            ps = conn.prepareStatement(sql);
//            ps.setString(1,"admin_01");
//            ps.setString(2,"002415");
//            ps.setString(3,"xx03");
//            实现更新
//            String sql = "update t_user set login_name = ? where login_name = ?";
//            ps = conn.prepareStatement(sql);
//            ps.setString(1,"fuuuuuk");
//            ps.setString(2,"admin_01");
//            实现删除
            String sql = "delete from t_user where login_name = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"fuuuuuk");
            //4.执行sql语句
            int count = ps.executeUpdate();
            System.out.println(count);
        }catch(Exception e){
            e.printStackTrace();

        }finally {
            if (rs != null){
                try{
                    rs.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }

            if (ps != null){
                try{
                    ps.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }

            if (conn != null){
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721684.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号