📄 buildwebmanage.java~1~
字号:
package servlet.buildweb;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import bean.BuildWebBean;
import bean.DomainBean;
import bean.MessageBoard;
import bean.Product;
import common.Database1;
public class BuildWebManage {
/**
* @param args
*/
protected java.sql.Connection userConn;
/**
*
* @param product
* @return maxid if add record successfully
*/
public int add(BuildWebBean buildWeb) {
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = isConnSupplied ? userConn : Database1.getConnection();
int maxid = 1;
String sql = "select max(webID) as maxid from ZNJZFW";
// 创建数据记录集对象:
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
maxid = rs.getInt(1) + 1;
}
sql = "insert into ZNJZFW(webID,webName,webImage,webIntr,webPrice)"
+ "values('"
+ maxid
+ "','"
+ buildWeb.getWebName()
+ "','"
+ buildWeb.getWebImage()
+ "','"
+ buildWeb.getWebIntr()
+ "','"
+ buildWeb.getWebPrice()
+ "' )";
// System.out.println(sql);
sql = new String(sql.getBytes("ISO8859-1"), "GBK");
// System.out.println(sql);
// 执行sql语句:
stmt.executeUpdate(sql);
return maxid;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -2;
} finally {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
public int getCount() {
// 创建数据库连接对象:
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
conn = isConnSupplied ? userConn : Database1.getConnection();
stmt = conn.createStatement();
// sql语句:
String sql = "select count(webID) from ZNJZFW";
// 执行sql语句:
// 执行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 {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
public Collection getAllMessages(int pagesize, int page) {
// 创建数据库连接对象:
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
conn = isConnSupplied ? userConn : Database1.getConnection();
stmt = conn.createStatement();
// sql语句:
String sql = "select top " +
pagesize + " webID,webName,webImage,webIntr,webPrice from ZNJZFW "
+ "where webID not in (select top "
+ pagesize*(page-1) + " webID from ZNJZFW order by webID)"
+ "order by webID ";
// 执行sql语句:
// 执行sql语句,返回一个记录集到rs:
rs = stmt.executeQuery(sql);
Collection c = new ArrayList();
BuildWebBean msg = null;
while (rs.next()) {
msg = new BuildWebBean();
msg.setWebID(String.valueOf(rs.getInt("webID")));
msg.setWebImage(rs.getString("webImage"));
msg.setWebIntr(rs.getString("webIntr"));
msg.setWebName(rs.getString("webName"));
msg.setWebPrice(rs.getDouble("webPrice"));
c.add(msg);
msg = null;
}
return c;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
public int delete(int id) {
// 创建数据库连接对象:
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
conn = isConnSupplied ? userConn : Database1.getConnection();
stmt = conn.createStatement();
// sql语句:
String sql = "delete from ZNJZFW where webID = '" + id + "'";
// 执行sql语句:
int i = stmt.executeUpdate(sql);
return i;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -2;
} finally {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
// public DomainBean getMessage(int id) {
// // 创建数据库连接对象:
// boolean isConnSupplied = (userConn != null);
// Connection conn = null;
// Statement stmt = null;
// ResultSet rs = null;
// try {
// // 创建数据记录集对象:
// conn = isConnSupplied ? userConn : Database1.getConnection();
// stmt = conn.createStatement();
//
// // sql语句:
// String sql = "select * from YMZCFW where domID='" + id + "'";
//
// // 执行sql语句,返回一个记录集到rs:
// rs = stmt.executeQuery(sql);
// DomainBean msg = null;
// if (rs.next()) {
// msg = new DomainBean();
// msg.setDomClass(rs.getString("domClass"));
// msg.setDomExample(rs.getString("domExample"));
// msg.setDomID(rs.getInt("domID"));
// msg.setSerClass(rs.getString("serClass"));
// }
// return msg;
// } catch (SQLException sqlExc) {
// sqlExc.printStackTrace();
// return null;
// } catch (Exception e) {
// e.printStackTrace();
// return null;
// } finally {
// Database1.close(stmt);
// if (!isConnSupplied) {
// Database1.close(conn);
// }
//
// }
// }
public int modify(BuildWebBean msg) {
// 创建数据库连接对象:
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
conn = isConnSupplied ? userConn : Database1.getConnection();
stmt = conn.createStatement();
// sql语句:
String sql = "update ZNJZFW set webName='"
+ msg.getWebName() + "',webIntr='"
+ msg.getWebIntr() + "',webPrice='"
+ msg.getWebPrice() + "',webImage='"
+ msg.getWebImage() + "' where webID='"
+ msg.getWebID() + "'";
// 执行sql语句:
sql = new String(sql.getBytes("ISO8859-1"), "GBK");
int i = stmt.executeUpdate(sql);
return i;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -2;
} finally {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
public BuildWebBean get(int id) {
// 创建数据库连接对象:
boolean isConnSupplied = (userConn != null);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建数据记录集对象:
conn = isConnSupplied ? userConn : Database1.getConnection();
stmt = conn.createStatement();
// sql语句:
String sql = "select * from ZNJZFW where webID = '" + id + "'";
// 执行sql语句:
rs = stmt.executeQuery(sql);
BuildWebBean buildWeb = null;
if (rs.next()) {
buildWeb = new BuildWebBean();
buildWeb.setWebID(String.valueOf(rs.getInt("webID")));
buildWeb.setWebImage(rs.getString("webImage"));
buildWeb.setWebIntr(rs.getString("webIntr"));
buildWeb.setWebName(rs.getString("webName"));
buildWeb.setWebPrice(rs.getDouble("webPrice"));
return buildWeb;
}
return buildWeb;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
Database1.close(stmt);
if (!isConnSupplied) {
Database1.close(conn);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -