1.1 JDBC的CRUD操作之PreparedStatement的添加数据操作
1.1.1 保存操作代码实现
- 已知表数据
package com.xdr630.jdbc.demo5;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import com.xdr630.jdbc.utils.JDBCUtils;
public class JDBCDemo5 {
@Test
public void demo1(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = JDBCUtils.getConnection();
String sql = "insert into user values (null,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "eee");
pstmt.setString(2, "abc");
pstmt.setString(3, "旺财");
pstmt.setInt(4, 32);
int num = pstmt.executeUpdate();
if(num > 0){
System.out.println("添加成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(pstmt, conn);
}
}
}