public int Getallemps () {
First way Pure JDBC mode
Connection Conn=null;
PreparedStatement Ps=null;
ResultSet Rs=null;
try {
Conn=c3pool.getconnection ();
String sql= "SELECT COUNT (*) from EMP";
PS = conn.preparestatement (SQL);
rs = Ps.executequery ();
int num=0;
while (Rs.next ()) {
Num=rs.getint (1);
// }
return num;
} catch (SQLException e) {
throw new RuntimeException (e);
}finally{
C3pool.close (Conn, PS, RS);
// }
The second way, with the help of the Dbutils tool
try {
Queryrunner qr=new Queryrunner (C3pool.getdatasource ());
String sql= "SELECT COUNT (*) from EMP";
BigDecimal big = (BigDecimal) qr.query (SQL, New Scalarhandler ());
int Num=big.intvalue ();
return num;
} catch (SQLException e) {
throw new RuntimeException (e);
}
}
-------------------Tool Class-----------------------
Package cn.gdpe.util;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class c3pool{
private static Combopooleddatasource Datasource=null;
How XML is configured
static{
Datasource=new Combopooleddatasource ();
}
public static Connection getconnection () {
Connection Conn=null;
try {
Conn=datasource.getconnection ();
Return conn;
} catch (SQLException e) {
E.printstacktrace ();
}
return null;
}
public static DataSource Getdatasource () {
return dataSource;
}
public static void Close (Connection conn,statement St) {
try {
if (conn!=null) {
Conn.close ();
}
if (st!=null) {
St.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
public static void Close (Connection conn,statement St, ResultSet rs) {
try {
if (rs!=null) {
Rs.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
Close (conn,st);
}
}
}
-----------------------C3PO configuration file-----------------------
<?xml version= "1.0" encoding= "UTF-8"?>
<c3p0-config>
<default-config>
<property name= "Driverclass" >oracle.jdbc.driver.OracleDriver</property>
<property name= "Jdbcurl" >jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name= "User" >scott</property>
<property name= "Password" >root</property>
<property name= "Initialpoolsize" >5</property>
<property name= "Maxpoolsize" >5</property>
<property name= "Minpoolsize" >1</property>
<property name= "Acquireincrement" >2</property>
</default-config>
</c3p0-config>
Two ways to get all the number of data bars in a table in a database