📄 publicfunction.java
字号:
package cn.edu.yuyh.tools;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
public class PublicFunction
{
Connection conn = null;
String CLASSFORNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVERANDDB = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=strutsexample";
String USERNAME = "sa";
String PASSWORD = "sa";
public Connection getConnection() throws Exception{
try {
Class.forName(CLASSFORNAME);
conn = DriverManager.getConnection(SERVERANDDB, USERNAME, PASSWORD);
}
catch (Exception e) {
e.printStackTrace();
throw e;
}
return conn;
}
public String trans2iso8859(String chi)
{
String result = null;
byte temp [];
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}
public static void error(HttpServletRequest httpServletRequest, String error0) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error", error0));
httpServletRequest.setAttribute("org.apache.struts.action.ERROR", errors);
}
public static void warn(HttpServletRequest httpServletRequest, String warn0) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("warn", warn0));
httpServletRequest.setAttribute("org.apache.struts.action.ERROR", errors);
}
public Vector initOcdeptno(HttpServletRequest httpServletRequest,Connection conn)throws Exception{
Vector vt = new Vector();
String sql = "select DeptNo,DeptName from Department";
ArrayList al = this.queryArrayList(sql, conn);
int size = al.size();
for (int i = 0; i < size; i++)
{
String label = (String) ( (HashMap) al.get(i)).get("DeptName");
String value = (String) ( (HashMap) al.get(i)).get("DeptNo");
vt.add(new org.apache.struts.util.LabelValueBean(label, value));
}
al = null;
sql = null;
return vt;
}
public Vector initOcspecialno(HttpServletRequest httpServletRequest,Connection conn)throws Exception{
Vector vt = new Vector();
String sql = "select SpecialNo+','+DeptNo SpecialNoAndDeptNo,SpecialName from Special";
ArrayList al = this.queryArrayList(sql, conn);
int size = al.size();
for (int i = 0; i < size; i++)
{
String label = (String) ( (HashMap) al.get(i)).get("SpecialName");
String value = (String) ( (HashMap) al.get(i)).get("SpecialNoAndDeptNo");
vt.add(new org.apache.struts.util.LabelValueBean(label, value));
}
al = null;
sql = null;
return vt;
}
public Vector initOcsclass(HttpServletRequest httpServletRequest,Connection conn)throws Exception{
Vector vt = new Vector();
String sql = "select distinct class from Student order by class";
ArrayList al = this.queryArrayList(sql, conn);
int size = al.size();
for (int i = 0; i < size; i++)
{
String label = (String) ( (HashMap) al.get(i)).get("class");
String value = (String) ( (HashMap) al.get(i)).get("class");
vt.add(new org.apache.struts.util.LabelValueBean(label, value));
}
al = null;
sql = null;
return vt;
}
/**
* 数据库查询:传入一个sql,返回一个ArrayList,ArrayList中存放的是HashMap
* @param sql String
* @param con Connection
* @throws Exception
* @return ArrayList
*/
public ArrayList queryArrayList(String sql, Connection con) throws
Exception {
ArrayList array = new ArrayList();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData rsm = rs.getMetaData();
int count = rsm.getColumnCount();
while (rs.next()) {
HashMap row = new HashMap();
for (int i = 1; i <= count; i++) {
row.put(rsm.getColumnLabel(i), rs.getObject(i));
}
array.add(row);
}
}
catch (Exception e) {
throw e;
}
return array;
}
public String trim(String str){
String temp = str;
if(temp==null)
return "";
else
return temp.trim();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -