preparedStatement插入

package com.song.testjdbc.lesson2;

import com.song.testjdbc.lesson2.utils.JdbcUtils;

import java.sql.*;

public class zhuru {
public static void main(String[] args) {
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
//区别 使用问号占位符代替参数
String sql = "INSERT into tb_user(userid,username,userPwd,userAge) VALUES (?,?,?,?)";

preparedStatement = connection.prepareCall(sql);//预编译
//手动赋值
preparedStatement.setInt(1,4444);
preparedStatement.setString(2,"陈欢");
preparedStatement.setString(3,"13");
preparedStatement.setString(4,"13");

int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println("执行成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
posted @ 2022-06-07 19:17  小松2739  阅读(52)  评论(0编辑  收藏  举报