package com.briup.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.briup.common.ConnectionFactory;
public class PreparedStatementTest {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
private Statement stmt;
public void insertListForPs(List<Person> list){
try {
conn = ConnectionFactory.getConnection();
String sql = "insert into person(id,name,age) values(?,?,?)";
ps = conn.prepareStatement(sql);
//设置自动提交为false
conn.setAutoCommit(false);
int i = 0;
for(Person p:list){
i++;
ps.setLong(1, p.getId());
ps.setString(2, p.getName());
ps.setInt(3, p.getAge());
//把数据添加到批处理中
ps.addBatch();
if(i==100){//30条一次批处理
//统一处理这个批处理
ps.executeBatch();
i=0;
}
}
ps.executeBatch();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ConnectionFactory.close(ps, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stmtForDate(){
try {
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement();
java.util.Date date = new Date();
// String sql = "insert into student(myday) values('23-8月-13')";
String sql = "insert into student(myday) values(to_date('12-02-13','dd-mm-yy'))";
System.out.println(sql);
stmt.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ConnectionFactory.close(stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void psForDate(){
try {
conn = ConnectionFactory.getConnection();
String sql = "insert into student(myday) values(?)";
ps = conn.prepareStatement(sql);
java.util.Date d1 = new java.util.Date();
long time = d1.getTime();
java.sql.Date d2 = new java.sql.Date(time);
java.sql.Timestamp timestamp = new Timestamp(time);
ps.setDate(1, d2);
// ps.setTimestamp(1, timestamp);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ConnectionFactory.close(ps, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void insertListForStms(List<Person> list){
try {
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement();
String sql = "";
for(Person p:list){
long id = p.getId();
String name = p.getName();
int age = p.getAge();
sql = "insert into person(id,name,age) values("+id+",'"+name+"',"+age+")";
stmt.execute(sql);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ConnectionFactory.close(stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void insert(Person p){
try {
conn = ConnectionFactory.getConnection();
String sql = "insert into person(id,name,age) values(?,?,?)";
ps = conn.prepareStatement(sql);
//用值把sql语句中的?替换掉
ps.setLong(1, p.getId());
ps.setString(2, p.getName());
ps.setInt(3, p.getAge());
ps.execute();//这时候一定不要把sql语句放进去 错的:ps.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ConnectionFactory.close(ps, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
PreparedStatementTest pst = new PreparedStatementTest();
// List<Person> list = new ArrayList<Person>();
// Person p = null;
// for(int i=1;i<2001;i++){
// p=new Person(i,"tom",20);
// list.add(p);
// }
// long start = System.currentTimeMillis();
//// pst.insertListForStms(list);438
// pst.insertListForPs(list);
// long end = System.currentTimeMillis();
// System.out.println(end-start);
// pst.psForDate();
pst.stmtForDate();
}
}
package com.briup.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class ConnectionFactory {
private static String driver ;
private static String url ;
private static String username ;
private static String password ;
static{
try {
Properties p = new Properties();
// p.load(new FileInputStream(new File("src/com/briup/common/oracle.properties")));
// p.load(ConnectionFactory.class.getResourceAsStream("oracle.properties"));
p.load(ConnectionFactory.class.getClassLoader().getResourceAsStream("com/briup/common/oracle.properties"));
driver = p.getProperty("driver");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");
// System.out.println(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
public static void close(ResultSet rs,Statement stmt,Connection conn) throws Exception{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
}
public static void close(Statement stmt,Connection conn) throws Exception{
close(null,stmt,conn);
}
// public static void main(String[] args) {
//
// }
}
分享到:
相关推荐
Connection
View Connection Server安装配置手册
代码如下:Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0 很明显这是连接初始化阶段就丢失了连接的错误。 google半天大多是说的注释掉配置文件中 bind-address = ...
mount.nfs: Connection timed out
解决oracle数据库发生'Connection reset by peer' or 'Connection reset'的错误 这边的数据库连接是kettle自带的h2数据库,只要有kettle就有这个数据库,目录位于data-integration\samples\db\,连接方式是 ...
Connection Emulator,SoftPerfect连接仿真器(SCE)软件的目的是为网络应用程序开发人员,系统管理员和网络工程师。软件开发人员在创建网络应用程序,尤其是那些对时间要求严格,如VoIP软件或实时协议,需要在各种...
Remote Desktop Connection Manager远程桌面连接管理器应用程序,可以让你管理多个远程桌面连接。Remote Desktop Connection Manager适用于用于机房中的服务器组,类似于内置的远程桌面MMC管理单元。
linux设置connection was reset
public static Connection getOracleConnection() { String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String user = "scott"; String password = "scott"; String Driver="oracle.jdbc.driver.OracleDriver...
mysql-connection jar包,jtsl-impl jar包,javax-servlet jar包,等等
Software caused connection abort: recv failed
VMware-Horizon-Connection-Server-x86_64-7.6.0-9823717.part2
Jack Audio Connection Kit官网源文件 你有没有想过把一个软件的音频输出发送给另一个?将同一个程序的输出结果发送给另外两个程序,然后将结果记录在第一个程序中,如何?或者,您可能是一位编写实时音频和音乐应用...
adodb Connection对象简介
USB HID device connection
putty connection refused问题的定位解决,putty connection refused问题的定位解决
connection对象的execute方法
The Network Adapter could not establish the connection