📄 citymsg.java
字号:
package com.wxpn.tutorial.ec.bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import com.wxpn.tutorial.db.ConnectionPool;
import com.wxpn.tutorial.db.DB;
/**
*
* @author
*/
public class CityMsg {
public int getId(String clause) {
// 创建数据库连接对象:
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
stmt = conn.createStatement();
// sql语句:
String sql = "select * from citytable "+clause+"";
// 执行sql语句:
rs = stmt.executeQuery(sql);
if (rs.next()) {
int id = rs.getInt("CITYID");
return id;
}
return 0;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
// 关闭连接,释放数据库资源:
try {
if (stmt != null) {
stmt.close();
}
connPool.freeConnection(conn);
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
}
}
}
public int getAll() {
// 创建数据库连接对象:
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
stmt = conn.createStatement();
// sql语句:
String sql = "select count(cityid) from citytable";
// 执行sql语句,返回一个记录集到rs:
rs = stmt.executeQuery(sql);
if (rs.next()) {
return rs.getInt(1);
}
return 0;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
// 关闭连接,释放数据库资源:
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
connPool.freeConnection(conn);
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
}
}
}
public int getCount() {
// 创建数据库连接对象:
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
stmt = conn.createStatement();
// sql语句:
String sql = "select count(cityid) from citytable";
// 执行sql语句,返回一个记录集到rs:
rs = stmt.executeQuery(sql);
if (rs.next()) {
return rs.getInt(1);
}
return 0;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
// 关闭连接,释放数据库资源:
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
connPool.freeConnection(conn);
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
}
}
}
public Collection getAll(String clause) {
// 创建数据库连接对象:
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
stmt = conn.createStatement();
// sql语句:
String sql = "select * from citytable "+clause+" order by id desc ";
// 执行sql语句,返回一个记录集到rs:
rs = stmt.executeQuery(sql);
Collection c = new ArrayList();
City city = null;
while (rs.next()) {
city = new City();
city.setId(rs.getInt("cityid"));
city.setCityName(rs.getString("cityname"));
city.setContent(rs.getString("citycontent"));
c.add(city);
city = null;
}
return c;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
// 关闭连接,释放数据库资源:
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
connPool.freeConnection(conn);
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -