必须使用Statement案例

package com.bjpowernode.jdbc;

import java.sql.*;

/**
 * @Author:杨青
 * @Time:2021/10/26 17:09
 * @Description:
 *      PreparedStatement完成insert delete update
 */
public class JDBCTest09 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            //1.类加载完成注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123456");
            //3.获取预编译数据库操作对象
            //String sql="insert into t_user (loginName,loginPwd,realName)values(?,?,?)";
            //String sql="update t_user set loginName='python',loginPwd='111',realName='Python' where id=4";
            String sql="delete from t_user where id=4 ";
            ps=conn.prepareStatement(sql);
            /*
            ps.setString(1,"java");
            ps.setString(2,"000");
            ps.setString(3,"Java");
            //4.执行sql语句
             */
            int count=ps.executeUpdate();
            System.out.println("执行语句条数:"+count);
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            //6.释放资源
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/-slz-2/p/15467652.html