zoukankan      html  css  js  c++  java
  • jdbc(预编译插入数据)

    package jdbc;

    import java.sql.*;

    public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //配置信息
    //useUnicode=true&characterEncoding=utf-8 解决中文乱码
    String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&serverTimezone=PRC";
    String username = "root";
    String password = "";

    //1加载驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    //2连接数据库
    Connection connection = DriverManager.getConnection(url,username,password);
    //3编写sql
    String sql = "insert into users(id,name,password,email,birthday)values (?,?,?,?,?)";
    //4预编译
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setInt(1,4);//第1个占位符?的值赋值为4
    ps.setString(2,"赵六");//第2个占位符?的值赋值为"赵六"
    ps.setString(3,"123456");//第3个占位符?的值赋值为"123456"
    ps.setString(4,"zl@qq.com");//第4个占位符?的值赋值为"zl@qq.com"
    ps.setDate(5,new Date(new java.util.Date().getTime()));//第5个占位符?的值赋值为"2000-08-09"
    //5执行sql
    int i = ps.executeUpdate();
    if (i>0){
    System.out.println("插入成功");
    }
    //6关闭连接,释放资源(一定要做)先开后关
    ps.close();
    connection.close();
    }
    }
  • 相关阅读:
    AIX 第3章 指令记录
    AIX 第2章 指令记录
    Oracle Exadata体系笔记
    决定undo表空间的大小
    摘录:官方文档对ROWID虚拟行的定义
    ORA-01102 cannot mount database in EXCLUSIVE mode
    居民身份证号码含义
    ORA-00257错误
    微机原理之 输入输出与中断
    操作系统总结之 输入输出系统(下)
  • 原文地址:https://www.cnblogs.com/liuyunche/p/14146336.html
Copyright © 2011-2022 走看看