Java数据库处理的方法库DBUtil
2010年07月16日 13:28 | 分类:测试开发蒋 刚毅
1.postgres数据库的lect操作,返回lect后返回的第一行数据松脂的意思,返回类型为string,若要返回int,只需将getString改为getInt
public static String PgDBSelect(String sql) throws ClassNotFoundException, SQLException{
just for fun String dbURL=”jdbc:postgresql://172.16.4.25:5432/skyups”;
String dbur=”***”;
String dbpasswd=”***”;
Class.forName(”org.postgresql.Driver”);
Connection conn = Connection(dbURL,dbur,dbpasswd);
Statement stmt = ateStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = uteQuery(sql);
rs.next();
String String(1);
rs.clo();
stmt.clo();
conn.clo();extensive
return value;
}
2.postgres数据的执行操作,可执行instert、delete、update等操作,不返回数据
public static void PgDBUpdate(String sql) throws ClassNotFoundException, SQLException{eblin
String dbURL=”jdbc:postgresql://172.16.4.25:5432/skyups”;
String dbur=”***”;
String dbpasswd=”***”hardest;
Class.forName(”org.postgresql.Driverdarkblue”);
Connection conn = Connection(dbURL,dbur,dbpasswd);
hash hash Statement stmt = ateStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
uteUpdate(sql);
stmt.clo();
conn.clo();
}
3. oracle数据库的lect操作,返回lect后返回的第一行数据,若要返回int,只需将getString改为getInt
public static String OracleDBSelect(String sql) throws ClassNotFoundException, SQLException{
String dbURL=”jdbc:oracle:thin:@172.16.4.21:1521:skytest”;
String dbur=”***”;
pets3成绩查询 String dbpasswd=”***”;
Class.forName(”oracle.jdbc.OracleDriver”);
Connection conn = Connection(dbURL,dbur,dbpasswd);
Statement stmt = ateStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = uteQuery(sql);
rs.next();
int String(1);
rs.clo();
stmt.clo();
conn.clo();
return expbalance;
}
4.oralce数据的执行操作,可执行instert、delete、update等操作,不返回数据
public static void OracleDBUpdate(String sql) throws ClassNotFoundException, SQLException{
String dbURL=”jdbc:oracle:thin:@172.16.4.21:1521:skytest”;
String dbur=”***”;
String dbpasswd=”***”;
Class.forName(”oracle.jdbc.OracleDriver”);
Connection conn = Connection(dbURL,dbur,dbpasswd);
Statement stmt = ateStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
uteUpdate(sql);
stmt.clo();
conn.clo();
}
5.Cassandra非关系型数据库的操作,包括delete、inrt、read等。
六级阅读分值 public static void CassandraRemove(String USkyid)throws IllegalStateException, PoolExhaustedException,Exception {
CassandraClientPool pool = ();
CassandraClient client = pool.borrowClient(”172.16.4.244″, 9160);
try{
Keyspace keyspace = Keyspace(”skyups”);
ColumnPath columnPath = new ColumnPath();
columnPath.tColumn_family(”ud toUrInfo”);
//delete
ve(USkyid, columnPath);
/*
// read
Column col = Column(value, columnPath);
System.out.println(”Read from cassandra: ” + Value()));
// inrt
keyspace.inrt(”key”, columnPath, bytes(”value”));
*/
} finally{
&leaClient(client);
}
}
利用方法库执行一次完整的junit测试举例:
step1.设置一条用户信息,包括所有字段参数。step2:再发送一条设置信息,其中用户的出生日期为空。step3:删除这条用户信息,恢复环境。
@Before
public void tUp() throws Exception {
Socket socket=new Socket(ServerIP,Port);
DataOutputStream dos=new OutputStream());
DataInputStream dis=new InputStream());
byte[] resp=new byte[1024];
System.out.println(”StartUp:预置测试环境,初始化设置1条用户信息“);
byte[] turinfomsg1=PackMsg.SetUrInfoMsg(USkyid, UUrName, UNickname, URealName, USex, UBirthday, UAnimals, UStar, UBlood, UMarried, UPortraitId, UDefinePortrait, UCountry, UProvince, UCity, UHometown, ULongitude, ULatitude, USignature, UDesc, UEmail, UEmailChecked, UMobile, UMobileChecked, UTelephone, UVocation, USchoolGraduated, UPrivacy, UIdCardNo,HasPic);
dos.write(turinfomsg1);ad(resp);
Thread.sleep(1000);
dos.flush();dis.clo();socket.clo();
}
@Test
public void testSetUrInfo_Edit_BirthDay() throws Exception {
Socket socket=new Socket(ServerIP,Port);
DataOutputStream dos=new OutputStream());
DataInputStream dis=new InputStream());
byte[] resp=new byte[1024];
System.out.println(”Ca1:执行测试,对出生日期为空字符串的逻辑进行修正,若为空字符串,则将原有的出生日期清空,设置为默认值1800-01-01“);
String UBirthday=” “;
byte[] turinfomsg1=PackMsg.SetUrInfoMsg_Birthday(USkyid, UBirthday);
dos.write(turinfomsg1);ad(resp);
int respcode2=ByteUtil.bytes2int2(resp, 40, 4);
System.out.println(”Respcode:”+respcode2);
asrtEquals (”Respcode Error!”,200,respcode2);
Thread.sleep(1000);
asrtEquals (”Birthday Error!”,”1800-01-01″,DBUtil.PgDBSelect(”lect birthday from skyups.tbl_ups_ur_info where skyid=”+USkyid));
dos.flush();dis.clo();socket.clo();
}