• PreparedStatement的用法


    package com.example.jdbc.origin;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Test {
    	public static void main(String[] args) throws SQLException {
            try
            {
                Class.forName("com.mysql.cj.jdbc.Driver");//加载并注册驱动程序
            }catch(ClassNotFoundException e)//加载错误,捕获异常
            {
                System.out.println("加载驱动失败");
            }
              	Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/user?Unicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC", "root","123456"); //创建Connection连接对象
              	String name="batch insert test name100";
              	String sql0="update t_person set job=(select count(*) from t_user where user_name='"+name+"') where name='"+name+"'";
              	Statement sta = con.createStatement();
              	System.out.println("sql0:"+sql0);
              	sta.execute(sql0);
              	String sql="update t_person set job=(select count(*) from t_user where user_name=?)as job where name=?";
            	sql="update t_person set job=? where name= ?";
            	System.out.println("sql:"+sql);
              	PreparedStatement stmt = con.prepareStatement(sql); //创建语句对象
              	stmt.setString(1, name);
              	stmt.setString(2, name);
              	//stmt.execute();
              	stmt.executeUpdate();
                con.close();
                 
        }
    
    }
    

      

  • 相关阅读:
    伪类样式
    div 文字超出边框后,省略号显示
    关于常用的 meta
    js数组去重
    异步二进制文件下载
    JJWT现前后端分离身份验证
    ApachePOI快速入门
    axios兼容ie7
    vue解决跨域问题
    log4j模板
  • 原文地址:https://www.cnblogs.com/JAYIT/p/13816571.html
Copyright © 2020-2023  润新知