Two ways to get all the number of data bars in a table in a database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.