jsp查询数据库代码

jsp查询数据库代码,第1张

这种方法并不是很常用,只是页面的相关 *** 作,代码太多,所以中间省略了部分<%

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>

String sql = "select from yonghu where sname = and spass = ";

现在用的判断,用户名及其密码是否正确,已经不用这个sql语句了;

这样效率太慢了(要把表中所有的内容都select出来,放到内存里)。

现在用:

String sql = "select count(sname) from yonghu where sname = and spass = ";

while (rsnext()) {

return true;

} 的意思是:如果用户名和密码都正确返回true

rsnext()) 返回的是一个boolean型,是说如果有下一行就返回true;

然后,在ResultSet rs = stmtexecuteQuery(); 句后面这么写

rsnext();

while(rsgetInt(1)==1){

return true;

}

而且,你的连接都没关,虽然也能正确运行,但是很浪费内存;应该关了:在return false; 前一行添上

if(rs!=null){

try{

rsclose();

}

catch(SQLException e){

}

}

if(stmt!=null){

try{

stmtclose();

}

catch(SQLException e){

}

}

if(con!=null){

try{

conclose();

}

catch(SQLException e){

}

}

import javaawt;

import javaxswing;

import javaawtevent;

import javaio;

public class Sy_8 extends JFrame

{

private static final long serialVersionUID = 1L;

private JTextField username;

private JPasswordField password;

private JButton logonButton;

private JButton cancelButton;

//登陆界面

public Sy_8(){

setTitle("登录");

setSize(300, 120);

setLayout(new GridLayout(3, 2, 2, 2));

setResizable(false);

Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();

Dimension frameSize = thisgetSize();

if (frameSizeheight > screenSizeheight) {

frameSizeheight = screenSizeheight;

}

if (frameSizewidth > screenSizewidth) {

frameSizewidth = screenSizewidth;

}

thissetLocation((screenSizewidth - frameSizewidth) / 2,

(screenSizeheight - frameSizeheight) / 2);

username = new JTextField(16);

password = new JPasswordField(16);

logonButton = new JButton("确定");

logonButtonaddActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent evt) {

if((usernamegetText()equals("123"))&&(passwordgetText()equals("123"))){

theMain them = new theMain();

themsetSize(500,450);

themshow();

}

}

});

cancelButton = new JButton("取消");

cancelButtonaddActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent evt) {

Systemexit(0);

}

});

add(new JLabel("用户名:"));

add(username);

add(new JLabel("密 码:"));

add(password);

add(logonButton);

add(cancelButton);

}

public static void main(String[] args) {

Sy_8 frame = new Sy_8();

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

frameshow();

}

}

//登陆后的界面

class theMain extends JFrame implements ActionListener

{

MenuBar myb = new MenuBar();

Menu f;

MenuItem stu_lr,stu_xs,guanyu,tuchu;

public theMain(){

setMenuBar(myb);

f = new Menu("文件");

stu_lr = new MenuItem("学生信息录入");

stu_xs = new MenuItem("学生信息显示");

guanyu = new MenuItem("关于(A)");

tuchu = new MenuItem("退出(E)");

fadd(stu_lr);

fadd(stu_xs);

fadd(guanyu);

fadd(tuchu);

mybadd(f);

stu_lraddActionListener(this);

stu_xsaddActionListener(this);

guanyuaddActionListener(this);

tuchuaddActionListener(this);

}

public void actionPerformed(ActionEvent e){

if(egetSource() == stu_lr){

Student stu = new Student();

stusetSize(385,205);

stushow();

stusetResizable(false);

}else if(egetSource() == stu_xs){

stu_display studis = new stu_display();

studissetSize(450,300);

studisshow();

studissetResizable(false);

}else if(egetSource() == guanyu){

}else if(egetSource() == tuchu){

Systemexit(0);

}

}

}

//学生信息录入

class Student extends JFrame implements ActionListener

{

Cal c = new Cal();

JButton save,next,back;

JLabel[] lab = new JLabel[5];

JTextField[] jst = new JTextField[5];

File file;

FileWriter fw;

PrintWriter out;

static int k;

public Student()

{

setLayout(new FlowLayout());

setTitle("学生基本信息录入");

String str ="学号:姓名:性别:年龄:备注:";

for (int i =0; i<5;i++){

lab[i] = new JLabel(strsubstring(3i,3i+3));

jst[i]=new JTextField(30);

add(lab[i]);

add(jst[i]);

}

save = new JButton("保存");

next = new JButton("下一个");

back = new JButton("返回");

add(save);

add(next);

add(back);

saveaddActionListener(this);

nextaddActionListener(this);

backaddActionListener(this);

}

public void actionPerformed(ActionEvent e){

if(egetSource() == save){

k++;

cseti(k);

try{

file = new File("studenttxt");

fw = new FileWriter(file,true);

out= new PrintWriter(fw);

for (int i=0;i<5;i++){

outprintln(jst[i]getText());

}

outclose();

}catch(IOException ee){

Systemoutprintln(ee);

}

}

if(egetSource() == next){

for(int i=0;i<5;i++){

jst[i]setText("");

}

}

if(egetSource()==back){

dispose();

}

}

}

//学生信息显示

class stu_display extends JFrame{

Cal c = new Cal();

JTextField[] lab = new JTextField[5];

File file;

FileReader fw;

BufferedReader in;

JPanel p,p1;

String s;

int pa = cgeti();

JTextField[] jst = new JTextField[pa];

public stu_display()

{

p = new JPanel();

psetLayout(new FlowLayout());

setTitle("学生基本信息显示");

String str ="学号姓名性别年龄备注";

for(int i=0;i<5;i++){

lab[i] = new JTextField(7);

lab[i]setText(" " + strsubstring(2i,2i+2));

lab[i]setEditable(false);

padd(lab[i]);

}

add(p,BorderLayoutCENTER);

try{

file = new File("studenttxt");

fw = new FileReader(file);

in = new BufferedReader(fw);

int i=0;

s=inreadLine();

while(s != null){

jst[i]=new JTextField(7);

jst[i]setText(s);

padd(jst[i]);

s=inreadLine();

i++;

}

inclose();

}catch(IOException e){

Systemoutprintln(e);

}

p1 = new JPanel();

p1setLayout(new BorderLayout());

JButton ok = new JButton("OK");

JButton o = new JButton("");

osetVisible(false);

p1add(o,BorderLayoutCENTER);

p1add(ok,BorderLayoutEAST);

add(p1,BorderLayoutSOUTH);

okaddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

dispose();

}

});

}

}

class Cal{

static int i,result;

public int geti(){

result= i5;

Systemoutprintln(result);

return result;

}

public void seti(int j){

i=j;

Systemoutprintln(i);

}

}

在使用带参数的sql时(sql server 2000),出现以下问题:

两种方法调用同一个数据库 *** 作函数,只是在红色部分有区别,即传递参数名时是否带"@"符号。

方法一在我本机测试无误,而用方法二时返出现错误:“必须声明变量@paraName”

而我同事机器正好和我的结果相反。

现在初步怀疑是数据库设置问题,因为我们用的不是同一个数据库,数据库版本都是sql server 2000 如果朋友们有知道此问题的请指教!

1 数据库 *** 作函数

2 /// <summary>

3 /// 向数据库里插长sql字段

4 /// </summary>

5 /// <param name="strSQL">SQL语句</param>

6 /// <param name="fs"></param>

7 /// <returns>影响的记录数</returns>

8 public static int ExecuteSqlInsertLongSql(string strSQL, string textValue, string strParaName)

9 {

10 string strConn = SystemConfigurationConfigurationSettingsAppSettings["sqlserver"];

11 using (SqlConnection connection = new SqlConnection(strConn))

12 {

13 SqlCommand cmd = new SqlCommand(strSQL, connection);

14 string paraName = strParaName;

15 SystemDataSqlClientSqlParameter Sqlpara = new SqlParameter();

16 SqlparaParameterName = paraName;

17 SqlparaSqlDbType = SystemDataSqlDbTypeText;

18 //oraparaOracleType = SystemDataOracleClientOracleTypeLongVarChar;

19 SqlparaSize = textValueLength + 1;//intMaxValue;//

20 SqlparaValue = textValueToString();

21 cmdParametersAdd(Sqlpara);

22 try

23 {

24 connectionOpen();

25 int rows = cmdExecuteNonQuery();

26 return rows;

27 }

28 catch (SystemDataSqlClientSqlException E)

29 {

30 strErrMsg = EMessage;

31 throw new Exception(EMessage);

32 }

33 finally

34 {

35 cmdDispose();

36 connectionClose();

37 }

38 }

39 }

40 使用方法一:

41 string strHtmlValue=thisHtmlEditor1TextReplace("'","''");

42 sql="insert into wjb(scrq,scr,wjsm,wjnr,wjlxbm,shzt,wjm) values('"+thislb_fbrqTextTrim()+"','"+userID+"','"+thistxt_btTextTrim()+"',@paraName,'03','2','"+str_slm+"')";

43

44

45 if(connExecuteSqlInsertLongSql(sql,strHtmlValue,"@paraName")==1)

46 //

47

48 使用方法二:

49 string strHtmlValue=thisHtmlEditor1TextReplace("'","''");

50

51 sql="insert into wjb(scrq,scr,wjsm,wjnr,wjlxbm,shzt,wjm) values('"+thislb_fbrqTextTrim()+"','"+userID+"','"+thistxt_btTextTrim()+"',@paraName,'03','2','"+str_slm+"')";

52

53

54 if(connExecuteSqlInsertLongSql(sql,strHtmlValue,"paraName")==1)

55 //

以上就是关于jsp查询数据库代码全部的内容,包括:jsp查询数据库代码、java 代码求助、java frame 类编写登陆界面!输入用户名:123密码:123。点击登陆按钮则登陆成功!输入其他显示登陆失败!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9754105.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存