⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultsettests.java

📁 Java Database connection pool
💻 JAVA
字号:
package poolman.tests.basic;import java.util.*;import java.sql.*;import javax.sql.*;import javax.naming.*;import javax.transaction.*;import java.rmi.RemoteException;import java.rmi.RMISecurityManager;import junit.framework.*;import com.codestudio.sql.*;import com.codestudio.util.JDBCPool;public class ResultSetTests extends TestCase {    private String selectSQL;    private String insertSQL;    private String deleteSQL;    public ResultSetTests(String name) {	super(name);    }    public static Test suite() {	return new TestSuite(ResultSetTests.class);    }    protected void setUp() {	this.selectSQL = "select * from neville";	this.insertSQL = "insert into neville values(232, 'ResultSetTestSuite')";	this.deleteSQL = "delete from neville where id = 232";    }        public void testGetResultSet() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement();	    res = stm.executeQuery(selectSQL);	    assertNotNull(res);	} catch (Exception e) {	    fail();	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testBeforeFirst() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_READ_ONLY);	    res = stm.executeQuery(selectSQL);	    res.beforeFirst();	    assert(res.isBeforeFirst());	} catch (Exception e) {	    fail();	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testAfterLast() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_READ_ONLY);	    res = stm.executeQuery(selectSQL);	    res.afterLast();	    assert(res.isAfterLast());	} catch (Exception e) {	    fail();	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testPrevious() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_READ_ONLY);	    res = stm.executeQuery(selectSQL);	    res.afterLast();	    while (res.previous()) {		int id = res.getInt(1);		String name = res.getString(2);	    }	    assert(res.isBeforeFirst());	} catch (Exception e) {	    fail();	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testNext() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_READ_ONLY);	    res = stm.executeQuery(selectSQL);	    while (res.next()) {		int id = res.getInt(1);		String name = res.getString(2);	    }	    assert(res.isAfterLast());	} catch (Exception e) {	    fail();	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testUpdateRow() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_UPDATABLE);	    res = stm.executeQuery(selectSQL);	    res.first();	    res.updateInt(1, 5);	    res.updateString(2, "updateRowTest"); 	    res.updateRow();	    assert(res.getInt(1) == 5);	} catch (Exception e) {	    System.out.println(e.getMessage());	    System.out.println(e);	    e.printStackTrace();	    fail(e.toString());	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testInsertRow() {	Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_UPDATABLE);	    res = stm.executeQuery(selectSQL);	    res.moveToInsertRow();	    res.updateInt(1, 518);	    res.updateString(2, "insertRowTest");	    res.insertRow();	    res.last();	    assert(res.getInt(1) == 518);	} catch (Exception e) {	    System.out.println(e.getMessage());	    e.printStackTrace();	    fail(e.toString());	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    public void testDeleteRow() {		Connection con = null;	Statement stm = null;	ResultSet res = null;	try {	    con = getConnection();	    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,				      ResultSet.CONCUR_UPDATABLE);	    res = stm.executeQuery("select * from neville where id=518");	    res.first();	    res.deleteRow();	    assert(res.rowDeleted());	} catch (Exception e) {	    fail(e.toString());	    e.printStackTrace();	}	finally {	    try {		JDBCPool.closeResources(stm, res);		con.close();	    } catch (Exception ee) {}	}    }    private Connection getConnection() 	throws SQLException {	// load the PoolMan JDBC Driver	try {	    Class.forName("com.codestudio.sql.PoolMan").newInstance();	} catch (Exception ex) {	    System.out.println("Could Not Find the PoolMan Driver. " +			       "Is PoolMan.jar in your CLASSPATH?");	    System.exit(0);	}	return DriverManager.getConnection("jdbc:poolman://testdb");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -