PrepardStatement实现数据库的添加操作
package com.cloud.preparedstatement.crud;
import com.cloud.statement.crud.StatementTest;
import org.junit.Test;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.SimpleTimeZone;
/*
* 使用PreparedStatement来替换Statement,实现对数据表的增删改查操作
*增删改,查
*/
public class PreparedStatementUpdateTest {
//向customers表中添加一条记录
@Test
public void testInsert(){
Connection conn = null;
PreparedStatement ps = null;
try {
//1.读取配置文件中的4个基本信息
// InputStream is = PreparedStatementUpdateTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.获取连接
conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
//4.预编译sql语句,返回PreparedStatement的实例
String sql = "insert into customers(name,email,birth)values(?,?,?)";
ps = conn.prepareStatement(sql);
//5.填充占位符
ps.setString(1,"哪吒");
ps.setString(2,"nazha@gmail.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse("1000-01-01");
ps.setDate(3,new Date(date.getTime()));
//6.执行操作
ps.execute();
System.out.println("插入数据成功!");
}catch (Exception e)
{
e.printStackTrace();
}finally {
//7.资源的关闭
try {
if (ps!=null)
ps.close();
}catch (SQLException e)
{
e.printStackTrace();
}
try{
if (conn!=null )
conn.close();
}catch (SQLException e)
{
e.printStackTrace();;
}
}
}
}
WX20191205-015951@2x.png
网友评论