public List executeQuery(String command){
ArrayList list = new ArrayList();
CharConvertor cc = new CharConvertor();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = ConnectionPool.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(command);ResultSetMetaData rsmd = rs.getMetaData();
while ( rs.next() ){
Map map = new HashMap();
//数据结果集从1开始
for ( int i = 1; i <= rsmd.getColumnCount(); i++ ){
map.put(rsmd.getColumnName(i), cc.e2c(rs.getString(i)==null ? "":rs.getString(i)));
}
list.add(map);
}
/*
* 对于为null的字段如何处理?
* 如果表中有字段为null则会执行出错
* 1;数据库是否允许为空,不从数据库本身来限制而是从程序来保证
* 2;添加针对这种异常的特殊处理方法
* 在这里我利用简单的语句 ? : 来把null变成空白;在程序中有许多地方会出这样那样的问题
* 特别是null和空白导致的问题需要特别注意
*/
}catch(SQLException sqle){
//
}finally{
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
//上面一样的,用这个只是让他无论如何都要关闭连接,错误处理就算了:)
}
}
return list;
}
package cn.qhang.common;import java.io.UnsupportedEncodingException;
/**
* @author Vulcan 用于字符编码转换,在数据库进或出之前 调用 TODO 启航网站新系统 (c)2005 www.qhang.cn
*/
public class CharConvertor {
String C_ENCODE = "GBK";String E_ENCODE = "ISO-8859-1";
public String c2e(String str) {
String str1 = null;
try {
str1 = new String(str.getBytes(C_ENCODE), E_ENCODE);
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return str1;
}public String e2c(String str) {
String str1 = null;
try {
str1 = new String(str.getBytes(E_ENCODE), C_ENCODE);
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return str1;
}
//}
public int addArticle(ArticleForm article){
String title = article.getTitle();
String categoryId = article.getCategoryId();
String addTime = article.getAddTime();
String content = article.getContent();
String source = article.getSource();//文章来源,比如教务处阿 等等,字符编码装换
String picture = article.getPicture();
String adminId = article.getAdminId();//只是管理员的id号,不用字符编码转换
String status = article.getStatus();
String sql = "INSERT INTO articles(title,categoryId,addtime,content,source,picture,"
+"adminId,status)VALUES('"+ title +"','" + categoryId + "','" + addTime +
"','" + content + "','" + source + "','" + picture + "','" + adminId +
"','" + status + "')";
return (stmts.executeUpdate(sql));
//当然成功了就会返回“1“,or not "0"
/*
* 晕阿~这种sql语句太难构建了吧,不过还好,写好了一劳永逸 :)
*/
}
String info1="中文数据信息";
String info = cc.c2e(info1);
String sql="INSERT INTO form.clumn = '" + info +"'";
//下面是执行的操作,省略了
jdbc:mysql://localhost/qhang?user=user&password=password
//poolman.xml
<datasource><!-- ============================== -->
<!-- Physical Connection Attributes -->
<!-- ============================== --><!-- Standard JDBC Driver info -->
<dbname>qhang</dbname>
<jndiName>jndi-qhang</jndiName>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost/qhang?useUnicode=true&characterEncoding=GBK</url><username>user</username>
<password>password</password><minimumSize>0</minimumSize>
<maximumSize>10</maximumSize>
<connectionTimeout>600</connectionTimeout>
<userTimeout>12</userTimeout>
<shrinkBy>10</shrinkBy><logFile>D:/mysql/poolman.log</logFile>
<debugging>true</debugging><!-- Query Cache Attributes-->
<cacheEnabled>false</cacheEnabled>
<cacheSize>20</cacheSize>
<cacheRefreshInterval>120</cacheRefreshInterval></datasource>
<datasource><!-- ============================== -->
<!-- Physical Connection Attributes -->
<!-- ============================== --><!-- Standard JDBC Driver info -->
<dbname>qhangin</dbname>
<jndiName>jndi-qhangin</jndiName>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost/qhang</url><username>user</username>
<password>password</password><minimumSize>0</minimumSize>
<maximumSize>10</maximumSize>
<connectionTimeout>600</connectionTimeout>
<userTimeout>12</userTimeout>
<shrinkBy>10</shrinkBy><logFile>D:/mysql/poolman.log</logFile>
<debugging>true</debugging><!-- Query Cache Attributes-->
<cacheEnabled>false</cacheEnabled>
<cacheSize>20</cacheSize>
<cacheRefreshInterval>120</cacheRefreshInterval></datasource>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionServlet;
/**
* @author vulcan
*
* TODO
* 覆盖父类中的process方法
*/
public class QhangCMSActionServlet extends ActionServlet{
protected void process(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException{
request.setCharacterEncoding("GBK");//如果想要支持国际化就要设置成UTF-8
//网页的编码也要设置成UTF-8
super.process(request, response);
}
}
<servlet-name>action</servlet-name>
<servlet-class>cn.qhang.common.QhangCMSActionServlet</servlet-class>
发表于 @ 2005年02月17日 16:27:00 | 评论( loading... ) | 举报| 收藏