
1tomcat55的下载地址
>
这种方法并不是很常用,只是页面的相关 *** 作,代码太多,所以中间省略了部分<%
Object userUID=sessiongetAttribute("USERID");
String uid="";
if(userUID!=null)
{
uid=(String)userUID;
}
else
{ responsesendRedirect("/Public/loseSessionjsp");}
try
{
String today=DateUtilsgetInstance()getToday();
String userIP=requestgetRemoteAddr();
String sql="";
String getPage=requestgetParameter("toPage");
sql=cuexchange(requestgetParameter("sql")); if (sql==null||sqlequals("")){
String getDepId="";
String getPoliticalPosition="";
String getTechnicalPosition="";
String getPoliticalLevel="";
String getTechnicalLevel="";
String getDegreeCode="";
String getGrade="";
String getLongevity="";String getAllowance="";
long depId=0;
long degreeCode=0;
long politicalPosition=0;
long technicalPosition=0;
long politicalLevel=0;
long technicalLevel=0;
long grade=0;
long longevity=0;
long allowance=0;
String[] postID=null;
String post="";
String userID="";
String userName="";
String address="";
getDepId=requestgetParameter("depId");
depId=LongparseLong(getDepIdtrim());getDegreeCode=requestgetParameter("degreeCode");
if (getDegreeCode!=null) {
degreeCode=LongparseLong(getDegreeCodetrim());
}
getPoliticalPosition=requestgetParameter("politicalPosition");
if (getPoliticalPosition!=null) {
politicalPosition=LongparseLong(getPoliticalPositiontrim());
}
post=requestgetParameter("post");
if(post!=null&&posttrim()equals("1"))
postID=requestgetParameterValues("dyourlocation");
getTechnicalPosition=requestgetParameter("technicalPosition");if (getTechnicalPosition!=null) {
technicalPosition=LongparseLong(getTechnicalPositiontrim());
}
getPoliticalLevel=requestgetParameter("politicalLevel");if (getPoliticalLevel!=null) {politicalLevel=LongparseLong(getPoliticalLeveltrim());
}getTechnicalLevel=requestgetParameter("technicalLevel");if (getTechnicalLevel!=null) {
technicalLevel=LongparseLong(getTechnicalLeveltrim());
}getGrade=requestgetParameter("grade");if (getGrade!=null) {
grade=LongparseLong(getGradetrim());
}getLongevity=requestgetParameter("longevity");if (getLongevity!=null) {
longevity=LongparseLong(getLongevitytrim());
}getAllowance=requestgetParameter("allowance");
if (getAllowance!=null) {
allowance=LongparseLong(getAllowancetrim());
}
userID=ParamUtilsgetParameter(request,"userID");//用户代码
userName=ParamUtilsgetParameter(request,"userName");
//用户名称
address=ParamUtilsgetParameter(request,"address");
//得到要转入的页面sql="select org_user from org_user,org_detail where org_useruser_id=org_detailuser_id";
if (depId!=0)
{
sql=sql+" and org_userdepartment_id="+depId+"";
}if (userID!=null&&userID!=""){
sql=sql+" and org_useruser_id='"+userID+"'";
}
if (userName!=null&&userName!="")
{
sql=sql+" and org_username like '%"+userName+"%'";
}if (address!=null&&address!="")
{
sql=sql+" and org_useraddress like '%"+address+"%'";
}
if(post!=null&&!posttrim()equals("0"))
{
sql=sql+" and org_detailpost in (";
for(int i=0;i<postIDlength;i++)
{
sql=sql+postID[i];
if(i!=(postIDlength-1))
sql=sql+",";
}
sql=sql+")";
}
if (degreeCode!=0)
{
sql=sql+" and org_detaildegree="+degreeCode+"";
}
if (politicalPosition!=0)
{
sql=sql+" and org_detailpoliticalPosition="+politicalPosition+"";
}
if (technicalPosition!=0)
{
sql=sql+" and org_detailtechnicalPosition="+technicalPosition+"";
}
if (technicalPosition!=0)
{
sql=sql+" and org_detailpoliticalLevel="+politicalLevel+"";
}
if (grade!=0)
{
sql=sql+" and org_detailgrade="+grade+"";
}
if (longevity!=0)
{
sql=sql+" and org_detaillongevity="+longevity+"";
}
if(getAllowance!=null)
{
if (allowance==0||allowance==1)
{
sql=sql+" and org_detailallowance="+allowance+"";
}
}
}
User[] userList =null;
userList=urcomplexSearch(sql);
logaddLog(1,1,1,uid,userIP,uid+"于("+today+")查询员工");int cnt=userListlength;//总记录数
int pageSize=10;//每页显示记录数
int curPage=1;//当前页
int cntPage;//总页数
int m=1; if (cnt>0)
{
if (cnt%pageSize==0)
cntPage=cnt/pageSize;
else
cntPage=cnt/pageSize+1;
}
else
cntPage=0; if (getPage==null)
{
getPage="1";
curPage=1;
}
else
curPage=IntegerparseInt(getPagetrim());
%><form name="thisform" action="userSearchDeljsp" method="post">
<table bgColor="#FFFFFF" border="1" borderColorDark="#ffffff" borderColorLight="#c0c0c0" cellSpacing="0" width="95%" cellpadding="4">
<tr height="25" align="center" bgcolor="#959595">
<td></td>
<td><font color="#FFFFFF">姓名</font></td>
<td><font color="#FFFFFF">性别</font></td>
<td> <font color="#FFFFFF">所在部门</font></td>
<td> <font color="#FFFFFF">办公地址</font></td>
<td><font color="#FFFFFF">联系电话</font></td>
</tr>
<%
int u=1;
for(int i=0;i<cnt;i++)
{
//显示记录的起始位置
int j=(curPage-1)pageSize;
//显示记录的末位置
int k=curPagepageSize;
//只显示page_size条数据
if(m>j&&m<=k)
{
String userId=userList[i]getUserID();
String gender=userList[i]getGender();
String name=userList[i]getName();
String tel=userList[i]getTel();
String useAddress=userList[i]getAddress();
long dep_id=userList[i]getDepID();
String dname=depgetName(userList[i]getDepID());
if(tel==null||telequals("null"))
tel="";
if(useAddress==null||useAddressequals("null"))
useAddress="";
//判断没一行该输出的颜色
if((u%2)==0)
{
outprintln("<tr bgcolor=#D7D7D7 onMouseOver=thisstylebackgroundColor='#fcd4d7'; onMouseOut=if(selectedItem!=thisid){thisstylebackgroundColor='#D7D7D7';}>");
}
else
{
outprintln("<tr onMouseOver=thisstylebackgroundColor='#fcd4d7'; onMouseOut=if(selectedItem!=thisid){thisstylebackgroundColor='#FFFFFF';}>");
}
u++;
%>
<td align=center><input type="checkbox" name="checkbox" id="CHK_+<%=userId%>" value="<%=userId%>"></td>
<td align=center>
<%
long departmentID=urgetDepID(uid);
if(uphaveRight(uid,"USER_COMPLEXQUERY")==true)
{
%>
<a href="userAnalyzejspuserId=<%=userId%>&dep_id=<%=dep_id%>&toPage=<%=curPage%>"><font color="#330099">
<%
}
else if(uphaveRight(uid,"DEPARTMENT_PLATFORM")==true&&dep_id==departmentID)
{
%>
<a href="userAnalyzejspuserId=<%=userId%>&dep_id=<%=dep_id%>&toPage=<%=curPage%>"><font color="#330099">
<%
}
%>
<%=name%></font></a></td>
<%
if(gendertrim()equals("0"))
outprintln("<td align=center>男</td>");
else
outprintln("<td align=center>女</td>");
%>
<td align=center><a href="depintrojspid=<%=dep_id%>&toPage=<%=curPage%>"><font color="#330099"><%=dname%></font></a></td>
<td align=center> <%=useAddress%></td>
<td align=center> <%=tel%></td>
</tr>
<%
}//end if m++;
}//end for
%>
<input type=hidden name="sql" value="<%=sql%>">
<input type=hidden name="page" value="<%=curPage%>">
</table>
</form>
<form name=pageform action="searchActionjsp" method="post">
<table border="0" width="95%">
<tr>
<td align="center">第<%=curPage%>页/共<%=cntPage%>页</td>
<td align="left">
<img name="firstpage" src="/Image/btn_firstpagegif" style="cursor:hand" onclick="pageClick(1)" <%=curPage>1" ":"disabled"%>>
<img name="prepage" src="/Image/btn_prepagegif" style="cursor:hand" onclick="pageClick(<%=curPage-1%>)" <%=curPage>1" ":"disabled"%>>
<img name="nextpage" src="/Image/btn_nextpagegif" style="cursor:hand" onclick="pageClick(<%=curPage+1%>)" <%=curPage<cntPage" ":"disabled"%>>
<img name="lastpage" src="/Image/btn_lastpagegif" style="cursor:hand" onclick="pageClick(<%=cntPage%>)" <%=curPage<cntPage" ":"disabled"%>>
</td>
<td align=left width=55%>
到第<input type="text" name="goPage" size="4">页
<img border="0" src="/Image/icon_searchgif" style="cursor:hand" onclick="aPage()">
</td>
</tr><tr>
<td colspan="3" valign="middle" width="23%" align="right">
<%
if(uphaveRight(uid,"USER_MANAGE")==true)
{
%>
<img border="0" src="/Image/btn_deletegif" style="cursor:hand" onclick="delUser()">
<%}%>
<img border="0" src="/Image/btn_returngif" style="cursor:hand" onclick="back()">
</td>
<input type=hidden name="sql" value="<%=sql%>">
<input type=hidden name="toPage">
</td>
</tr>
</table>
</form>
<br>
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
}//end else
}//end try
catch(Exception e)
{
outprintln(egetMessage());
}
%>
</td>
</tr>
</table>
数据库连接类:
package cnhpubbsutil;import javasqlConnection;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javasqlStatement;
public class DB {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "commysqljdbcDriver" ;
//定义mysql的数据库连接地址:
public static final String DBDURL = "jdbc:mysql://localhost/bbs2014" ;
//mysql数据库的连接用户名
public static final String DBUSER = "root" ;
//mysql数据库的连接密码
public static final String DBPASS = "1234" ;
public static Connection createConn(){
Connection conn =null;
try {
ClassforName(DBDRIVER);
conn=DriverManagergetConnection(DBDURL,DBUSER,DBPASS);
} catch (ClassNotFoundException e) {
eprintStackTrace();
} catch (SQLException e) {
eprintStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn,String sql){
PreparedStatement ps=null;
try {
ps=connprepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return ps;
}
public static void close(Connection conn){
if(conn==null) return;
try {
connclose();
conn=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
public static void close(Statement stmt){
if(stmt==null) return;
try {
stmtclose();
stmt=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
public static void close(ResultSet rs){
if(rs==null) return;
try {
rsclose();
rs=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
Category的一个JavaBean:
package cnhpubbsmodel;public class Category {
private int id;
private String name;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
thisdescription = description;
}
}
对数据库和Category的 *** 作类://说白了就是增删查修
<pre name="code" class="java">package cnhpubbsservice;import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilArrayList;
import javautilList;
import cnhpubbsmodelCategory;
import cnhpubbsutilDB;
public class CategoryService {
public void add(Category c){
Connection conn=DBcreateConn();
String sql="insert into category (name,description) values (,)";
PreparedStatement ps=DBprepare(conn, sql);
try {
pssetString(1, cgetName());
pssetString(2, cgetDescription());
psexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
}
DBclose(ps);
DBclose(conn);
}
public List<Category> list(){
Connection conn=DBcreateConn();
String sql="select from category";
PreparedStatement ps=DBprepare(conn, sql);
List<Category> categories=new ArrayList<Category>();
try {
ResultSet rs=psexecuteQuery();
Category c=null;
while(rsnext()){
c=new Category();
csetId(rsgetInt("id"));
csetName(rsgetString("name"));
csetDescription(rsgetString("description"));
categoriesadd(c);
}
} catch (SQLException e) {
eprintStackTrace();
}
DBclose(ps);
DBclose(conn);
return categories;
}
public void delete(Category c){
deleteById(cgetId());
}
public void deleteById(int id){
Connection conn=DBcreateConn();
String sql="delete from category where id=";
PreparedStatement ps=DBprepare(conn, sql);
try {
pssetInt(1, id);
psexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
}
DBclose(ps);
DBclose(conn);
}
public void update(Category c){
Connection conn=DBcreateConn();
String sql="update category set name = , description = where id = ";
PreparedStatement ps=DBprepare(conn, sql);
try {
pssetString(1, cgetName());
pssetString(2, cgetDescription());
pssetInt(3, cgetId());
psexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
}
DBclose(ps);
DBclose(conn);
}
public Category loadById(int id){
Connection conn=DBcreateConn();
String sql="select from category where id=";
PreparedStatement ps=DBprepare(conn, sql);
Category c=null;
try {
pssetInt(1, id);
ResultSet rs=psexecuteQuery();
if(rsnext()){
c=new Category();
csetId(rsgetInt("id"));
csetName(rsgetString("name"));
csetDescription(rsgetString("description"));
}
} catch (SQLException e) {
eprintStackTrace();
}
DBclose(ps);
DBclose(conn);
return c;
}
}
一般的原则是页面不直接调用底层方法,如果你只是练习的话就再页面上直接调用也可以,写在<%%>中,和java中的使用方法一样,new对象,使用哪个方法,需要导包,java也一样,导包可以既在myeclipse中alt+/组合键自动导,也可以在头部手动写。如楼上。
个人建议你在sevlet里面调用,这个就是单纯的java调用了。然后用request等内置对象存,页面去取就可以了。这个也符合MVC的设计思想。实际开发中也会这么做。没有人会给jsp页面中写一堆java代码的。初学应该养成良好的习惯。
以上就是关于jsp如何连接数据库JDBC的配置全部的内容,包括:jsp如何连接数据库JDBC的配置、jsp代码连接数据库 如何按关键字搜索内容、jsp查询数据库代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)