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();
}
}