public class JDBC implements AutoCloseable {
public Connection conn;
// private Statement statement;
private PreparedStatement pstmt;
private ResultSet rs;
private int updateResult;
public static DataSource ds = null; /**
* 默认的构造函数
*/ public JDBC() {
try {
conn = ConnectionFactory.getConnection();
if (!conn.isClosed()) {
Kit.log.debug("与数据库成功创建连接!");
}
System.out.println("初始化完成");
} catch (SQLException ex) {
Kit.log.warn("异常:数据库连接发生错误:\n " + ex.toString());
}
}
public boolean addNodeComsume(int nodemac,int rgbbefore,Timestamp lastquerytime) throws SQLException
{
Timestamp time = new Timestamp(System.currentTimeMillis());
pstmt=conn.prepareStatement("UPDATE node_consume SET consume=consume+?,lastquerytime=? WHERE opentime IS NOT NULL AND closetime IS NULL AND nodemac=?");
pstmt.setTimestamp(2, time);
pstmt.setInt(1, (int)1);
//-lastquerytime.getTime())*rgbbefore);
pstmt.setInt(3, nodemac);
updateResult=pstmt.executeUpdate();
if (updateResult==1) {
return true;
}else {
return false;
}
}
public boolean addNodeComsumetest(int nodemac,int rgbbefore,Time lastquerytime) throws SQLException
{
Timestamp time = new Timestamp(System.currentTimeMillis());
pstmt=conn.prepareStatement("UPDATE node_consume SET consume=consume+(UNIX_TIMESTAMP(?)-UNIX_TIMESTAMP(lastquerytime))*?,lastquerytime=? WHERE opentime IS NOT NULL AND closetime IS NULL AND nodemac=?");
pstmt.setTimestamp(1, time);
pstmt.setInt(2, rgbbefore);
pstmt.setTimestamp(3,time);
pstmt.setInt(4, nodemac);
updateResult=pstmt.executeUpdate();
if (updateResult==1) {
return true;
}else {
return false;
}
}}junit代码
import static org.junit.Assert.*;import java.sql.SQLException;
import java.sql.Timestamp;import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;import com.thingcom.tools.JDBC;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class jdbctest { @Test
public void adgdNodeConsume() throws SQLException
{
JDBC jd=new JDBC();
Timestamp timestamp=new Timestamp(100000);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
}
@Test
public void adfdNodeComsumetest() throws SQLException
{ JDBC jd=new JDBC();
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
}
@Test
public void actestssss() {
// TODO Auto-generated method stub
JDBC jd=new JDBC();
}}测试结果,adfdNodeComsumetest用时0.12s左右,adgdNodeConsume用时0.6s,怎么胡差的那么多
public Connection conn;
// private Statement statement;
private PreparedStatement pstmt;
private ResultSet rs;
private int updateResult;
public static DataSource ds = null; /**
* 默认的构造函数
*/ public JDBC() {
try {
conn = ConnectionFactory.getConnection();
if (!conn.isClosed()) {
Kit.log.debug("与数据库成功创建连接!");
}
System.out.println("初始化完成");
} catch (SQLException ex) {
Kit.log.warn("异常:数据库连接发生错误:\n " + ex.toString());
}
}
public boolean addNodeComsume(int nodemac,int rgbbefore,Timestamp lastquerytime) throws SQLException
{
Timestamp time = new Timestamp(System.currentTimeMillis());
pstmt=conn.prepareStatement("UPDATE node_consume SET consume=consume+?,lastquerytime=? WHERE opentime IS NOT NULL AND closetime IS NULL AND nodemac=?");
pstmt.setTimestamp(2, time);
pstmt.setInt(1, (int)1);
//-lastquerytime.getTime())*rgbbefore);
pstmt.setInt(3, nodemac);
updateResult=pstmt.executeUpdate();
if (updateResult==1) {
return true;
}else {
return false;
}
}
public boolean addNodeComsumetest(int nodemac,int rgbbefore,Time lastquerytime) throws SQLException
{
Timestamp time = new Timestamp(System.currentTimeMillis());
pstmt=conn.prepareStatement("UPDATE node_consume SET consume=consume+(UNIX_TIMESTAMP(?)-UNIX_TIMESTAMP(lastquerytime))*?,lastquerytime=? WHERE opentime IS NOT NULL AND closetime IS NULL AND nodemac=?");
pstmt.setTimestamp(1, time);
pstmt.setInt(2, rgbbefore);
pstmt.setTimestamp(3,time);
pstmt.setInt(4, nodemac);
updateResult=pstmt.executeUpdate();
if (updateResult==1) {
return true;
}else {
return false;
}
}}junit代码
import static org.junit.Assert.*;import java.sql.SQLException;
import java.sql.Timestamp;import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;import com.thingcom.tools.JDBC;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class jdbctest { @Test
public void adgdNodeConsume() throws SQLException
{
JDBC jd=new JDBC();
Timestamp timestamp=new Timestamp(100000);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
jd.addNodeComsume(-49675966, 55, timestamp);
}
@Test
public void adfdNodeComsumetest() throws SQLException
{ JDBC jd=new JDBC();
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
jd.addNodeComsumetest(-49675966, 55, null);
}
@Test
public void actestssss() {
// TODO Auto-generated method stub
JDBC jd=new JDBC();
}}测试结果,adfdNodeComsumetest用时0.12s左右,adgdNodeConsume用时0.6s,怎么胡差的那么多
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货