关于 if(rs.getString("").trim().equals(""))

sdds123sdds 2012-11-26 03:24:44
为什么IF里面的语句没有被执行,之前定义bzhgl=100;运行后没有报错但bzhgl的值不变.
请各位大神们帮忙看看 ~~
<tr>
<td align="center" id="zhsjrq"><%=rq1%></td>
<td align="center" id="zhsjscx"><%=rs.getString("banbie")%></td>
<td align="center" id="zhsjscx"><%=rs.getString("banci")%></td>
<td align="center" id="zhsjscx"><%=rs.getString("shengchanxian")%></td>
<td align="center" id="zhsjgg"><%=rs.getString("guige")%></td>
<td align="center" id="zhsjcl"><%=hgp%></td>
<td align="center" id="zhsjcphgl"><%=dd.format(benbanchanpinhegelv)%></td>
<%

if(rs.getString("guige").trim().equals("PSP110-0.8(冷改穿)"))
{
bzhgl = 64.40;
}
if(rs.getString("guige").trim().equals("PSP63-0.6(冷改穿)"))
{
bzhgl = 62.55;
}
if(rs.getString("guige").trim().equals("PSP75-0.6(冷改穿)"))
{
bzhgl = 57.47;
}
if(rs.getString("guige").trim().equals("PSP90-0.8(冷改穿)"))
{
bzhgl = 52.25;
}
if(rs.getString("guige").trim().equals("PSP110-1.0(冷改穿)"))
{
bzhgl = 67.15;
}
if(rs.getString("guige").trim().equals("PSP110-1.2(冷改穿)"))
{
bzhgl = 70.27;
}
if(rs.getString("guige").trim().equals("普通PSP160-1.2(冷改穿)"))
{
bzhgl = 89.67;
}
if(rs.getString("guige").trim().equals("PSP160-1.4(冷改穿)"))
{
bzhgl = 89.23;
}
if(rs.getString("guige").trim().equals("PSP160-1.5(冷改穿)"))
{
bzhgl = 83.14;
}
if(rs.getString("guige").trim().equals("PSP160-1.7(冷改穿)"))
{
bzhgl = 77.55;
}
if(rs.getString("guige").trim().equals("普通PSP200-1.5(冷改穿)"))
{
bzhgl = 75.32;
}
if(rs.getString("guige").trim().equals("HPSP50-0.6(热改穿)"))
{
bzhgl = 51.67;
}
if(rs.getString("guige").trim().equals("HPSP63-0.6(热改穿)"))
{
bzhgl = 61.90;
}
if(rs.getString("guige").trim().equals("HPSP75-0.6(热改穿)"))
{
bzhgl = 48.29;
}
if(rs.getString("guige").trim().equals("HPSP90-0.8(热改穿)"))
{
bzhgl = 47.35;
}
if(rs.getString("guige").trim().equals("HPSP110-0.8(热改穿)"))
{
bzhgl = 54.99;
}
if(rs.getString("guige").trim().equals("HPSP110-1.0(热改穿)"))
{
bzhgl = 61.73;
}
if(rs.getString("guige").trim().equals("HPSP110-1.2(热改穿)"))
{
bzhgl = 65.74;
}
if(rs.getString("guige").trim().equals("HPSP160-1.4(热改穿)"))
{
bzhgl = 81.84;
}
if(rs.getString("guige").trim().equals("HPSP160-1.5(热改穿)"))
{
bzhgl = 76.04;
}
if(rs.getString("guige").trim().equals("HPSP160-1.7(热改穿)"))
{
bzhgl = 71.67;
}

%>
<td align="center" id="zhsjbzhgl"><%=dd.format(bzhgl)%></td>
<td align="center" id="zhsjsybhgl"><%=dd.format(100-bzbl-jybl-sbbl-gybl)%></td>
<td align="center" id="zhsjbzbl"><%=dd.format(bzbl)%></td>
<td align="center" id="zhsjsbbl"><%=dd.format(sbbl)%></td>
<td align="center" id="zhsjjybl"><%=dd.format(jybl)%></td>
<td align="center" id="zhsjgybl"><%=dd.format(gybl)%></td>
<td align="center" id="zhsjsjwh"><%=dd.format(sjwh)%></td>
<td align="center" id="zhsjdh"><%=dd.format(dianhao)%></td>
</tr>
...全文
630 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdds123sdds 2012-12-04
  • 打赏
  • 举报
回复 1
谢谢各位大虾指点,用了上面的办法都没有成功,我现在把" "去掉了,程序能够正常运行了.
dw_java08 2012-11-29
  • 打赏
  • 举报
回复
在jsp页面比较字符串是用"=="进行比较的。 改成这样 if(rs.getString("guige").trim() == "PSP110-0.8(冷改穿)")
sdds123sdds 2012-11-29
  • 打赏
  • 举报
回复
上面那个输出我打错了 没有引号的.我也复制粘贴过了,还是FALSE...我下午把他们的字符串长度输出看看是不是有问题....谢谢给的提示.我下午有时间再调试下
nmyangym 2012-11-29
  • 打赏
  • 举报
回复
对不起,楼主已经显示了输出。那有没有边上的 " 号啊? 有的话,代码里的那个字符串的要加\". 或者,干脆把输出的内容粘贴到代码里。
nmyangym 2012-11-29
  • 打赏
  • 举报
回复
把:rs.getString("guige").trim()得到的字符串输出,仔细看一下,应该好解决了。
sdds123sdds 2012-11-29
  • 打赏
  • 举报
回复
我加输出语句了结果显示是false.... rs.getString("guige").trim()显示为"PSP110-0.8(冷改穿)",一模一样为什么就不等啊
sdds123sdds 2012-11-29
  • 打赏
  • 举报
回复
谢谢,我明天再调试下
Geek41 2012-11-29
  • 打赏
  • 举报
回复
public static void main(String[] args){ System.out.println(" zz".trim().equals("zz")); System.out.println(" zz".trim().equals(" zz")); }
nmyangym 2012-11-29
  • 打赏
  • 举报
回复
那这样试试:

 if(rs.getString("guige").trim().replaceAll(" ","").equals("PSP110-0.8(冷改穿)"))
sdds123sdds 2012-11-29
  • 打赏
  • 举报
回复
额 确定.trim()不能去掉  ?
qinzi_2013 2012-11-29
  • 打赏
  • 举报
回复
Geek41 2012-11-29
  • 打赏
  • 举报
回复
trim()去掉的是空格, 不行
sdds123sdds 2012-11-29
  • 打赏
  • 举报
回复
还是不行, 是不是因为我数据库录入的是  PSP110-0.8(冷改穿)  ? .trim()能去掉  不能?
ebonyzhang 2012-11-27
  • 打赏
  • 举报
回复
引用 5 楼 nmyangym 的回复:
你在代码里加个输出,看看语句 if(rs.getString("guige").trim().equals("PSP110-0.8(冷改穿)")) 是否是true.
对的,先按这种方法排查一下!看看究竟获取到值没。如果是false说明你后台处理有问题的!
nmyangym 2012-11-27
  • 打赏
  • 举报
回复
你在代码里加个输出,看看语句 if(rs.getString("guige").trim().equals("PSP110-0.8(冷改穿)")) 是否是true.
sdds123sdds 2012-11-27
  • 打赏
  • 举报
回复
有木有人啊...........
sdds123sdds 2012-11-27
  • 打赏
  • 举报
回复
谢谢!~ 我去试下
sdds123sdds 2012-11-26
  • 打赏
  • 举报
回复
Double bzhgl = 100 ; if(rs.getString("guige").trim().equals("PSP110-0.8(冷改穿)")) { bzhgl = 64.40; } 当rs.getString("guige")是PSP110-0.8(冷改穿)的时候,bzhgl还是100......
yxhshuaiman 2012-11-26
  • 打赏
  • 举报
回复
哥 以后用equals() 时要注意了 if("HPSP160-1.7(热改穿)".equals(rs.getString("guige").trim()))这种写法才是对的 你那样容易报空指针异常 关于问题 没有看懂,代码太乱了
sdds123sdds 2012-11-26
  • 打赏
  • 举报
回复
自己先顶下......求解决
java 数据库 重点是实现了预约 违章等功能 public static int checkIt(String name, String password,String purview)// 验证用户名 密码 { interFace s = new interFace(); String temp = "select * from user_info " + "where user_name='" + name + "'" + " and " + "user_psw='" + password + "'" + " and " + "user_purview='" + purview + "'"; s.search(temp); int i = 0; try { while (s.C.next()) { i++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } public static void update_book_number(String s4){ String sql="update book_info set book_num_in=book_num_in+1 where book_info.book_id='"+s4+"'"; try{ interFace.edit(sql); }catch(Exception e){ e.printStackTrace(); } } public static int book_num_in(String s){ String sql="select book_num_in from book_info where book_name= '"+s+"'"; int count=0; try{ rs=B.executeQuery(sql); if(rs.next()){ count=Integer.parseInt(rs.getString(1)); } } catch(Exception e){ e.printStackTrace(); } return count; } public static void day_number(String s1,String s2,int s3){ float days=0;String s=null; int mum=0; try{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); s = df.format(new Date()); String date=s1; Date d1 = df.parse(s); Date d2 = df.parse(date); long diff = d1.getTime() - d2.getTime(); days= diff / (1000 * 60 * 60 * 24); }catch(Exception e){ e.printStackTrace(); }System.out.println(days); days=days-30; if(s2.equals("是")){ days=days-30; } if(days>0){ days=days/10;} else{ days=0; } if(days>0){ write_dirty(s3,days);//写入不良信息 } try{//把borrow 中的borrow_returned 将return 改成"是" String ss="是"; String sql="update borrow_info set borrow_returned='"+ss+ "' , borrow_info.return_date= '"+s+"'where borrow_info.borrow_id='"+s3+"'"; interFace.edit(sql); }catch(Exception e ){ e.printStackTrace(); } } public static void write_dirty(int s,float days){ try{ int count=0; rs=B.executeQuery("select count(*) from dirty_info "); if(rs.next()) count=Integer.parseInt(rs.getString(1)); count++; String des="过期"+days*10+"天,罚款"+days+"元"; interFace.edit("insert into dirty_info values('"+ count+"','"+s+"','"+des+"') "); JOptionPane.showMessageDialog(null,des,"提示!",JOptionPane.PLAIN_MESSAGE); String descri=Adminfrm.jTextArea.getText(); if(descri.trim().equals(null)){ //do nothing } else{ count++; descri=descri+"罚款一元"; interFace.edit("insert into dirty_info values('"+ count+"','"+s+"','"+descri+"') "); JOptionPane.showMessageDialog(null,descri,"提示!",JOptionPane.PLAIN_MESSAGE); } } catch(Exception e ){ e.printStackTrace(); } } public int user_num(){ interFace s = new interFace(); String temp = "select * from user_info " ; s.search(temp); int i1 = 0; try { while (s.C.next()) { i1++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i1; } public static int sum_borrow(){ int count=0; try{ String sql1="select max(borrow_id) from borrow_info"; rs=null; rs=B.executeQuery(sql1); while(rs.next()){ count= Integer.parseInt(rs.getString(1)); } } catch (Exception e ){ e.printStackTrace(); } count++; return count; } public static void edit(String command) { interFace s=new interFace (); s.C=s.search(command); return; } public static void book_inquire(Object [] [] data,String string1,String sql){ try{ rs=null; rs=B.executeQuery(sql); int i=0; initialize(data,30,9); while(rs.next()){ s1=rs.getString("book_id"); data[i][0]=s1; s2=rs.getString("book_name");data[i][1]=s2; s3=rs.getString("book_price").trim(); data[i][2] =s3; s4=rs.getString("book_publishing").trim();data[i][3]=s4; s5=rs.getString("book_author").trim();data[i][4]=s5; s6=rs.getString("book_date").trim();data[i][5]=s6; s7=rs.getString("book_num").trim();data[i][6]=s7; s8=rs.getString("book_num_in").trim();data[i][7]=s8; s9=rs.getString("lib_name");data[i][8]=s9; i++; } } catch(Exception e ){ e.printStackTrace(); } } public static String[][] doSearch(String command, String[] colName) { interFace s = new interFace(); String temp = command; s.C = s.search(temp); int col = colName.length, i = 0; s.rec = new String[100][col]; try { while (s.C.next()) { for (int j = 1; j <= col; j++) { s.rec[i][j - 1] = s.C.getString(j); } i++; s.i++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[][] result = new String[s.i][col]; for (int k = 0; k < s.i; k++) { for (int l = 0; l < col; l++) result[k][l] = s.rec[k][l]; } return result; } public static void inquire_history(String string1){ String m=string1; String sql="select * from borrow_info ,book_info,user_info where borrow_info.borrow_bid=book_info.book_id and borrow_uid=user_id and user_name='"+m+"'"; try{ rs=null; rs=B.executeQuery(sql); // System.out.println(rs); int i=0; initialize(Userfrm.rowData1,30,9); while(rs.next()){ if(!rs.getString("borrow_returned").trim().equals("否")){ s1=rs.getString("borrow_id"); Userfrm.rowData1[i][0]=s1; s2=rs.getString("borrow_uid");Userfrm.rowData1[i][1]=s2; s3=rs.getString("borrow_bid").trim(); Userfrm.rowData1[i][2]=s3; s4=rs.getString("book_name").trim();Userfrm.rowData1[i][3]=s4; s5=rs.getString("borrow_time").trim();Userfrm.rowData1[i][4]=s5; s6=rs.getString("borrow_reborrow").trim(); Userfrm.rowData1[i][5]=s6; s7=rs.getString("borrow_returned").trim(); Userfrm.rowData1[i][6]=s7; s8=rs.getString("return_date").trim();Userfrm.rowData1[i][7]=s8;} else{ continue; } i++; } } catch(Exception e ){ e.printStackTrace(); } } public static void inquire_current_history(String string1){ String m=string1; String sql="select * from borrow_info ,book_info,user_info where borrow_info.borrow_bid=book_info.book_id and borrow_uid=user_id and user_name='"+m+"'"; try{ rs=null; rs=B.executeQuery(sql); // System.out.println(rs); int i=0; initialize(Userfrm.rowData4,30,9); while(rs.next()){ if(rs.getString("borrow_returned").trim().equals("否")){ s1=rs.getString("borrow_id"); Userfrm.rowData4[i][0]=s1; s2=rs.getString("borrow_uid");Userfrm.rowData4[i][1]=s2; s3=rs.getString("borrow_bid").trim(); Userfrm.rowData4[i][2]=s3; s4=rs.getString("book_name").trim();Userfrm.rowData4[i][3]=s4; s5=rs.getString("borrow_time").trim();Userfrm.rowData4[i][4]=s5; s6=rs.getString("borrow_reborrow").trim(); Userfrm.rowData4[i][5]=s6; s7=rs.getString("borrow_returned").trim(); Userfrm.rowData4[i][6]=s7; s8=rs.getString("return_date").trim(); Userfrm.rowData4[i][7]=s8; } else{ continue; } i++; } } catch(Exception e ){ e.printStackTrace(); } } public static void inquire_weizhang(String string1){ String sql="select * from book_info ,borrow_info,dirty_info where book_info.book_id=borrow_info.borrow_bid " + "and borrow_info.borrow_id=dirty_info.dirty_borrow_id"; try{ rs=null; rs=B.executeQuery(sql); int i=0; initialize(Userfrm.rowData2,13,6); /**if(rs==null){ System.out.println("恭喜您,没有不良记录,请继续保持"); }*/ while(rs.next()){ s1=rs.getString("dirty_id"); Userfrm.rowData2[i][0]=s1; s2=rs.getString("borrow_id"); Userfrm.rowData2[i][1]=s2; s3=rs.getString("borrow_bid"); Userfrm.rowData2[i][2]=s3; s4=rs.getString("book_name"); Userfrm.rowData2[i][3]=s4; s5=rs.getString("borrow_time"); Userfrm.rowData2[i][4]=s5; s6=rs.getString("dirty_description"); Userfrm.rowData2[i][5]=s6; i++; } } catch(Exception e ){ e.printStackTrace(); } } //得出uid号 public static void book_request(String string1){ int count=0; int uid = 0; String s="是"; try{ String sql="select count (*) from request_info "; rs=null; rs=B.executeQuery(sql); while(rs.next()){ count= Integer.parseInt(rs.getString(1)); } String username=Login.jTextField.getText(); String sql1="select user_id from user_info where user_name='"+username+"'"; rs=null; rs=B.executeQuery(sql1); //System.out.println(rs); while(rs.next()){ uid= Integer.parseInt(rs.getString(1)); } Calendar cal=Calendar.getInstance(); String date=Integer.toString(cal.get(Calendar.YEAR))+"-"+ Integer.toString(cal.get(Calendar.MONTH)+1)+"-"+ Integer.toString(cal.get(Calendar.DATE)); count++; sql="insert into request_info values ( '"+count+"','"+uid+"','"+string1+"','"+date+"','"+s+"' )"; interFace.edit(sql); } catch (Exception e){ e.printStackTrace(); } } public static void yuyue_history(){ String username=Login.jTextField.getText(); int uid=0; String sql1="select user_id from user_info where user_name='"+username+"'"; try{rs=null; rs=B.executeQuery(sql1); //System.out.println(rs); while(rs.next()){ uid= Integer.parseInt(rs.getString(1)); } String sql="select * from request_info where request_uid ='"+uid+"'"; rs=null;int i=0; rs=B.executeQuery(sql); initialize(Userfrm.rowData3,10,5); // System.out.println(rs); while(rs.next()){ s1=rs.getString("request_id"); Userfrm.rowData3[i][0]=s1; s2=rs.getString("request_uid"); Userfrm.rowData3[i][1]=s2; s3=rs.getString("request_description"); Userfrm.rowData3[i][2]=s3; s4=rs.getString("request_date"); Userfrm.rowData3[i][3]=s4; s5=rs.getString("request_finished"); Userfrm.rowData3[i][4]=s5; i++; } } catch (Exception e){ e.printStackTrace(); } } public static void information(){ //System.out.println(Login.jTextField.getText()); String m=Login.jTextField.getText(); String sql="select * from user_info where user_name= '"+m+"'"; try{ rs=null; rs=B.executeQuery(sql); while(rs.next()){ s1=rs.getString("user_name"); Userfrm.jTextField3.setText(s1); s2=rs.getString("user_psw"); Userfrm.jTextField4.setText(s2); s3=rs.getString("user_age"); Userfrm.jTextField5.setText(s3); s4=rs.getString("user_sex"); Userfrm.jTextField6.setText(s4); } } catch (Exception e){ e.printStackTrace(); } } public static void info_modify(String s1,int a ,String b){ String m=Login.jTextField.getText(); String sql="update user_info set user_psw='"+s1 +"',user_age='"+a+"' , user_sex='"+b+"' where user_name='"+m+"'"; try{ interFace.edit(sql); } catch (Exception e ){ e.printStackTrace(); } } public static int whichlib(String s){ int w=0; try{ String sql1="select * from lib_info where lib_name='"+s+"'"; rs=null; rs=B.executeQuery(sql1); while(rs.next()){ w= Integer.parseInt(rs.getString(1)); } } catch (Exception e ){ e.printStackTrace(); } return w; } public static int book_sum(){ int count=0; try{ String sql1="select count(*) from book_info"; rs=null; rs=B.executeQuery(sql1); while(rs.next()){ count= Integer.parseInt(rs.getString(1)); } } catch (Exception e ){ e.printStackTrace(); } count++; return count; } public static void insert_book(String sql){ try{ rs=null; interFace.edit(sql); } catch (Exception e ){ e.printStackTrace(); } } public static void user_inquire(String sql){ try{ int i=0; rs=B.executeQuery(sql); while(rs.next()){ s1=rs.getString(2); Adminfrm.rowdata1[i][0]=s1; s2=rs.getString(10); Adminfrm.rowdata1[i][1]=s2; s3=rs.getString(5); Adminfrm.rowdata1[i][2]=s3; s4=rs.getString(6); Adminfrm.rowdata1[i][3]=s4; s5=rs.getString(7); Adminfrm.rowdata1[i][4]=s5; s6=rs.getString(8); Adminfrm.rowdata1[i][5]=s6; i++; } } catch (Exception e ){ e.printStackTrace(); } } public static void weizhang(){ String sql="select * from dirty_info"; try{ int i=0; rs=B.executeQuery(sql); initialize(Adminfrm.rowdata3,15,3); while(rs.next()){ s1=rs.getString(1); Adminfrm.rowdata3[i][0]=s1; s2=rs.getString(2);Adminfrm.rowdata3[i][1]=s2; s3=rs.getString(3).trim(); Adminfrm.rowdata3[i][2]=s3; i++; } } catch(Exception e ){ e.printStackTrace(); } } public static Boolean notrequest(String s){ Boolean re=false; String sql="select * from request_info where request_description='"+s+"' and request_finished='否'"; try{ rs=B.executeQuery(sql); if(!(rs.next())){ re=true; } }catch(Exception e){ e.printStackTrace(); } return re; } public static void request(){ String sql="select * from request_info"; try{ int i=0; rs=B.executeQuery(sql); initialize(Adminfrm.rowdata2,15,5); while(rs.next()){ s1=rs.getString(1); Adminfrm.rowdata2[i][0]=s1; s2=rs.getString(2);Adminfrm.rowdata2[i][1]=s2; s3=rs.getString(3).trim(); Adminfrm.rowdata2[i][2]=s3; s4=rs.getString(4).trim();Adminfrm.rowdata2[i][3]=s4; s5=rs.getString(5).trim();Adminfrm.rowdata2[i][4]=s5; i++; } } catch(Exception e ){ e.printStackTrace(); } } @SuppressWarnings("deprecation") public static void book_reborrow(String s){ String sql=null; String sql1=null; String username=Login.jTextField.getText(); //得出用户ID号 int u_id=0; String b_id=null; sql="select book_id from book_info where book_name='"+s+"'"; sql1="select user_id from user_info where user_name='"+username+"'"; try{ rs=null; rs=B.executeQuery(sql1); //System.out.println(rs); while(rs.next()){ u_id= Integer.parseInt(rs.getString(1)); } rs=null; rs=B.executeQuery(sql); if(rs.next()) b_id=rs.getString(1).trim(); int count=book_num_in(s); if((count>0)||(count==0&¬request(s) )){ String date; sql="select borrow_reborrow,return_date from borrow_info where borrow_bid='"+b_id+"' and borrow_uid='"+u_id+"'"; rs=B.executeQuery(sql); if(rs.next()){ if (rs.getString(1).trim().equals("否")){ //System.out.println(rs.getString(1)); date=rs.getString("return_date"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d=sdf.parse(date); System.out.println(d.toString()); /*String date=Integer.toString(cal.get(Calendar.YEAR))+"-"+ Integer.toString(cal.get(Calendar.MONTH))+"-"+ Integer.toString(cal.get(Calendar.DATE));*/ Calendar cal = new GregorianCalendar(); cal.setTime(d); String d1=Integer.toString(cal.get(Calendar.YEAR))+"-"+ Integer.toString(cal.get(Calendar.MONTH)+2)+"-"+ Integer.toString(cal.get(Calendar.DATE)); //System.out.println(d.getYear()+" "+d.getDay()); System.out.println(d1); sql="update borrow_info set borrow_reborrow= '是' , return_date='"+d1+"' where borrow_bid='"+b_id+"' and borrow_uid='"+u_id+"'"; System.out.println(sql); interFace.edit(sql); JOptionPane.showMessageDialog(null,"续借成功","提示!",JOptionPane.PLAIN_MESSAGE); } else JOptionPane.showMessageDialog(null,"已经续借,不得续借","提示!",JOptionPane.PLAIN_MESSAGE); } } else{ JOptionPane.showMessageDialog(null,"不得续借"+count + notrequest(s),"提示!",JOptionPane.PLAIN_MESSAGE); } }catch(Exception e){ e.printStackTrace(); } } @SuppressWarnings("static-access") public static void book_borrow(String b_id,int u_id){ interFace s=new interFace(); String sql="select count(*)from request_info where (request_finished='否' and request_uid<>'"+u_id+"' and " + " request_description in (select book_info.book_name " + "from book_info where book_id='"+b_id+"'))"; try{ rs=null;int count=0;int count1=0; rs=B.executeQuery(sql); if(rs.next()){ count=rs.getInt(1); rs=B.executeQuery("select book_num_in from book_info where book_id='"+b_id+"'"); if(rs.next()) count1=Integer.parseInt(rs.getString(1)); if(count>=count1||count1==0){System.out.println(count + " "+count1); JOptionPane.showMessageDialog(null,"全部书被预约或部借出","提示!",JOptionPane.PLAIN_MESSAGE); } else{ sql="update request_info set request_finished='是' where (request_uid='"+u_id+"' and " + "request_info.request_description=(select book_info.book_name " + "from book_info where book_id='"+b_id+"'))"; s.edit(sql); Calendar cal=Calendar.getInstance(); String date=Integer.toString(cal.get(Calendar.YEAR))+"-"+ Integer.toString(cal.get(Calendar.MONTH)+1)+"-"+ Integer.toString(cal.get(Calendar.DATE)); cal.add(Calendar.DATE, 30); String date1=Integer.toString(cal.get(Calendar.YEAR))+"-"+ Integer.toString(cal.get(Calendar.MONTH)+1)+"-"+ Integer.toString(cal.get(Calendar.DATE)); int num=s.sum_borrow(); sql="insert into borrow_info values('"+num+"','"+u_id+"','"+b_id+"','"+date+"','否','否','"+date1+"')"; s.edit(sql); sql="update book_info set book_info.book_num_in=book_info.book_num_in-1 " + "where book_id='"+b_id+"'"; s.edit(sql); JOptionPane.showMessageDialog(null,"成功借出","提示!",JOptionPane.PLAIN_MESSAGE); } } } catch (Exception e){ e.printStackTrace(); } } 课程设计 图书馆管理信息系统
package contorl; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JOptionPane; import model.DBManager; import view.DelUserPane; public class DUControl implements ActionListener { private DelUserPane du; private DBManager db=new DBManager(); private ResultSet rs; int eid=0; /** * @param pane */ public DUControl(DelUserPane pane) { du=pane; // TODO 自动生成构造函数存根 } /* (非 Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { // TODO 自动生成方法存根 Object btn=e.getSource(); String uid=du.numbertex.getText().trim(); String sql="select * from users where uid="; if(uid.equals("")) { JOptionPane.showMessageDialog(null,"请输入用户编号"); return; } if(btn==du.findbtn) { eid=Integer.parseInt(uid); du.delbtn.setEnabled(true); du.cancelbtn.setEnabled(true); dofine(sql); return; } if(btn==du.cancelbtn) { du.nametex.setText(""); du.nametex.setText("" + ""); du.deptex.setText(""); du.notetex.setText(""); return; } if(btn==du.delbtn) { boolean success=false; String name=du.nametex.getText().trim(); String dep=du.deptex.getText().trim(); String note=du.notetex.getText().trim(); if(name.equals("")||dep.equals("")) { JOptionPane.showMessageDialog(null,"请把信息填完整"); return; } int confirm=JOptionPane.showConfirmDialog(null,"是否删除?","删除确认",JOptionPane.YES_NO_OPTION); if(confirm==JOptionPane.YES_OPTION) { sql="update users set udel=0 where uid="+eid; System.out.println(sql); success=db.executeSql(sql); if(!success) { JOptionPane.showMessageDialog(null,"删除不成功,请重试"); du.delbtn.setEnabled(false); du.cancelbtn.setEnabled(false); return; } else { JOptionPane.showMessageDialog(null,"删除成功"); du.nametex.setText(""); du.numbertex.setText(""); du.deptex.setText(""); du.notetex.setText(""); du.delbtn.setEnabled(false); du.cancelbtn.setEnabled(false); return; } } } // TODO 自动生成方法存根 } private void dofine(String sql) { sql=sql+eid; rs=db.getResult(sql); try { if(!rs.first()|(rs.getInt(5)==0)) { JOptionPane.showMessageDialog(null,"没有该用户或者已经被删除"); du.delbtn.setEnabled(false); du.numbertex.setText(""); du.cancelbtn.setEnabled(false); return; } else { rs.beforeFirst(); while(rs.next()) { du.nametex.setText(rs.getString(2)); du.deptex.setText(rs.getString(3)); du.notetex.setText(rs.getString(4)); } } }catch(SQLException sqle) { JOptionPane.showMessageDialog(null,"没有该用户或者已经被删除"); System.out.println(sqle); du.numbertex.setText(""); du.delbtn.setEnabled(false); du.cancelbtn.setEnabled(false); return; } } }
import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import javax.swing.event.*; import javax.swing.border.*; import java.sql.*; import java.io.*; import java.net.*; import javax.swing.*; public class Java{ public static void main(String args[]){ One one=new One(); } } class One extends JFrame implements ActionListener{ JLabel jlabel1,jlabel2; JButton jbutton1,jbutton2; JTextField jtextfield1; JPanel jpanel1; JPasswordField passwordText; One(){ jpanel1=new JPanel(); jlabel1=new JLabel("用户名:"); jlabel2=new JLabel("密 码:"); jbutton1=new JButton("登录"); jbutton2=new JButton("取消"); Font f=new Font("楷体_GB2312",Font.BOLD,22); jlabel1.setFont(f); jlabel2.setFont(f); jtextfield1=new JTextField(10); passwordText=new JPasswordField(10); passwordText.setEchoChar('*'); jbutton1.addActionListener(this); jbutton2.addActionListener(this); setLayout(new FlowLayout()); jpanel1.add(jbutton1); jpanel1.add(jbutton2); add(jlabel1); add(jtextfield1); add(jlabel2); add(passwordText); add(jpanel1); setBounds(100,100,200,200); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbutton1){ String str1,str2; str1=jtextfield1.getText().trim(); str2=passwordText.getText(); Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM login"); boolean boo=true; while(rs.next()){ if(rs.getString(1).equals(str1) && rs.getString(2).equals(str2)){ JOptionPane.showMessageDialog(this,"欢迎进入图书管理系统","login",JOptionPane.PLAIN_MESSAGE); setVisible(false); FrameMain t=new FrameMain(); boo=false; } } if(boo){ JOptionPane.showMessageDialog(this,"用户不存在或者输入错误请重新输入!","提示",JOptionPane.WARNING_MESSAGE); jtextfield1.setText(""); passwordText.setText(""); } con.close(); }catch(SQLException eee){System.out.println(eee);} } else{ System.exit(0); } } } class FrameMain extends JFrame implements ActionListener{ JPanel contentPane; JMenuBar mb1=new JMenuBar(); JMenu readr=new JMenu("读者"); JMenu book=new JMenu("图书"); JMenu help=new JMenu("帮助"); JMenu exit=new JMenu("退出"); JMenuItem r1=new JMenuItem("浏览读者信息"); JMenuItem b1=new JMenuItem("浏览图书信息"); JMenuItem b2=new JMenuItem("增加图书"); JMenuItem b3=new JMenuItem("删除图书"); JMenuItem b4=new JMenuItem("修改图书信息"); JMenuItem h1=new JMenuItem("使用网络信息帮助"); JMenuItem h2=new JMenuItem("读取java文件帮助信息"); JMenuItem e1=new JMenuItem("退出系统"); FrameMain(){ setTitle("图书馆信息管理系统"); readr.add(r1); book.add(b1); book.add(b2); book.add(b3); book.add(b4); help.add(h1); help.add(h2); exit.add(e1); mb1.add(readr); mb1.add(book); mb1.add(help); mb1.add(exit); setJMenuBar(mb1); r1.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); h1.addActionListener(this); h2.addActionListener(this); e1.addActionListener(this); setBounds(200,200,500,500); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==r1){ new showReader(); } else if(e.getSource()==b1){ new showBook(); } else if(e.getSource()==b2){ new InsertBook(); } else if(e.getSource()==b3){ new Delete(); } else if(e.getSource()==b4){ new Modify(); } else if(e.getSource()==h1){ Help1 flink=new Help1(); } else if(e.getSource()==h2){ class Help2 extends JFrame { Help2(){ byte b[]=new byte[30]; JTextArea text=new JTextArea(20,20); setSize(330,300); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); add(text,BorderLayout.CENTER); try{ FileInputStream input=new FileInputStream("help.java"); ProgressMonitorInputStream in=new ProgressMonitorInputStream(this,"读取help文件",input); ProgressMonitor p=in.getProgressMonitor(); while(in.read(b)!=-1){ String s=new String(b); text.append(s); Thread.sleep(200); } }catch(InterruptedException e){} catch(IOException e){} } } new Help2(); } else if(e.getSource()==e1){ System.exit(0); } } } class Help1 extends JFrame implements ActionListener,Runnable{ JButton button; URL url; JTextField text; JEditorPane editPane; byte b[]=new byte[118]; Thread thread; public Help1(){ text=new JTextField(20); editPane=new JEditorPane(); editPane.setEditable(false); button=new JButton("确定"); button.addActionListener(this); thread=new Thread(this); JPanel p=new JPanel(); p.add(new JLabel("输入网址: ")); p.add(text); p.add(button); Container con=getContentPane(); con.add(new JScrollPane(editPane),BorderLayout.CENTER); con.add(p,BorderLayout.NORTH); setBounds(60,60,500,500); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); editPane.addHyperlinkListener(new HyperlinkListener (){ public void hyperlinkUpdate(HyperlinkEvent e){ if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ try{ editPane.setPage(e.getURL()); }catch(IOException e1){ editPane.setText(""+e1); } } } }); } public void actionPerformed(ActionEvent e){ if(!(thread.isAlive())) thread=new Thread(this); try{ thread.start(); }catch(Exception ee){ text.setText("我正在读取"+url); } } public void run(){ try{ int n=-1; editPane.setText(null); url=new URL(text.getText().trim()); editPane.setPage(url); }catch(MalformedURLException e1){ text.setText(""+e1); return; }catch(IOException eee){ text.setText(""+eee); return; } } } class InsertBook extends JDialog implements ActionListener{ JLabel insertBook; Object name[]={"编号","书名","出版社","出版日期"}; Object a[][]=new Object[1][4]; JTable table; JButton insert; Connection con; Statement sql; ResultSet rs; InsertBook(){ insertBook=new JLabel("请输入新书信息:"); table=new JTable(a,name); insert=new JButton("确定插入"); setLayout(new FlowLayout()); add(insertBook); add(table); add(insert); setBounds(100,100,800,200); setVisible(true); insert.addActionListener(this); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); int k=sql.executeUpdate("INSERT INTO bookinformation VALUES('"+a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')"); if(k==1) JOptionPane.showMessageDialog(this,"插入成功","成功",JOptionPane.PLAIN_MESSAGE); setVisible(false); con.close(); } catch(SQLException ee){ JOptionPane.showMessageDialog(this,"插入失败","失败",JOptionPane.ERROR_MESSAGE); } } } class showReader extends JDialog implements ActionListener{ JTable table; Object a[][]; Object name[]={"学号","姓名","年龄","出生日期"} ; JButton showReader; showReader(){ showReader=new JButton("显示信息"); showReader.addActionListener(this); add(showReader,BorderLayout.NORTH); setBounds(200,200,500,500); setVisible(true); } public void actionPerformed(ActionEvent e){ Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery("SELECT * FROM readerinformation"); rs.last(); int lastNumber=rs.getRow(); a=new Object[lastNumber][4]; int k=0; rs.beforeFirst(); while(rs.next()){ a[k][0]=rs.getString(1); a[k][1]=rs.getString(2); a[k][2]=rs.getString(3); a[k][3]=rs.getString(4); k++; } con.close(); }catch(SQLException ee){ System.out.println(ee); } table=new JTable(a,name); getContentPane().removeAll(); add(showReader,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER); setVisible(true); validate(); } } class showBook extends JDialog implements ItemListener{ JTable table; Object a[][]; Object name[]={"编号","书名","出版社","出版日期"} ; JCheckBox showBook; showBook(){ showBook=new JCheckBox("显示信息"); showBook.addItemListener(this); add(showBook,BorderLayout.NORTH); setBounds(200,200,500,500); setVisible(true); } public void itemStateChanged(ItemEvent e){ JCheckBox box=(JCheckBox)e.getItemSelectable(); if(box.isSelected()){ Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery("SELECT * FROM bookinformation"); rs.last(); int lastNumber=rs.getRow(); a=new Object[lastNumber][4]; int k=0; rs.beforeFirst(); while(rs.next()){ a[k][0]=rs.getString(1); a[k][1]=rs.getString(2); a[k][2]=rs.getString(3); a[k][3]=rs.getString(4); k++; } con.close(); }catch(SQLException ee){ System.out.println(ee); } table=new JTable(a,name); getContentPane().removeAll(); add(showBook,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER); setVisible(true); validate(); } else table.setVisible(false); } } class Delete extends JDialog implements ActionListener{ JLabel input; JTextField inputNumber; Object name[]={"编号","书名","出版社","出版日期"} ; Object a[][]=new Object[1][4]; JTable table; JButton delete; JPanel pane=new JPanel(); Connection con; Statement sql; ResultSet rs; String num; Delete(){ input=new JLabel("请输入删除书的编号:(以回车结束)"); inputNumber=new JTextField(20); delete=new JButton("删除"); table=new JTable(a,name); pane.add(input); pane.add(inputNumber); add(pane,BorderLayout.NORTH); add(table,BorderLayout.CENTER); add(delete,BorderLayout.SOUTH); inputNumber.addActionListener(this); delete.addActionListener(this); setBounds(100,100,800,300); setVisible(true); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException eee){ System.out.println(""+eee); } try{ num=inputNumber.getText().trim(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT *FROM bookinformation where 编号='"+num+"'"); boolean boo=rs.next(); if(boo==false){ JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE); } else{ a[0][0]=rs.getString(1); a[0][1]=rs.getString(2); a[0][2]=rs.getString(3); a[0][3]=rs.getDate(4).toString(); table.repaint(); if(e.getSource()==delete){ int n=JOptionPane.showConfirmDialog(this,"确认要删除吗?","确认对话框",JOptionPane.YES_OPTION); if(n==JOptionPane.YES_OPTION){ sql=con.createStatement(); int k=sql.executeUpdate("DELETE FROM bookinformation WHERE 编号='"+num+"'"); if(k==1) { JOptionPane.showMessageDialog(this,"删除成功!","删除",JOptionPane.PLAIN_MESSAGE); setVisible(false); } else { JOptionPane.showMessageDialog(this,"删除失败!","删除",JOptionPane.ERROR_MESSAGE); setVisible(false); } } } } con.close(); }catch(SQLException ee){ System.out.println(ee); } } } class Modify extends JDialog implements ActionListener{ JLabel infor; JTextField inputNumber; Object name[]={"编号","书名","出版社","出版日期"}; Object a[][]=new Object[1][4]; JTable table; JButton queren; Connection con; Statement sql; ResultSet rs; String num; Modify(){ infor=new JLabel("输入编号(以回车确认):"); inputNumber=new JTextField(20); table=new JTable(a,name); queren=new JButton("修改记录"); setLayout(new FlowLayout()); infor.setPreferredSize(new Dimension(100,70)); add(infor); add(inputNumber); add(table); queren.setPreferredSize(new Dimension(80,50)); add(queren); inputNumber.addActionListener(this); queren.addActionListener(this); setBounds(20,60,300,500); setVisible(true); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } if(e.getSource()==inputNumber){ try{ num=inputNumber.getText().trim(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM bookinformation WHERE 编号='"+num+"'"); boolean boo=rs.next(); if(boo==false){ JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE); } else{ a[0][0]=rs.getString(2); a[0][1]=rs.getString(3); a[0][2]=rs.getDate(4).toString(); table.repaint(); } con.close(); } catch(SQLException eee){ System.out.println(eee); } } if(e.getSource()==queren){ try{ table.repaint(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); int k=sql.executeUpdate("UPDATE bookinformation SET 书名='"+a[0][0]+"',出版社='"+a[0][1]+"',出版日期 ='"+a[0][2]+"' WHERE 编号='"+num+"'"); if(k==1){ JOptionPane.showMessageDialog(this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE); con.close(); setVisible(false); } }catch(SQLException eeee){ JOptionPane.showMessageDialog(this,"更新失败"+eeee,"失败",JOptionPane.ERROR_MESSAGE); } } } }
package dao.imp; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import common.Pager; import dao.BaseDao; import dao.UserDao; import entity.User; public class UserDaoImpl extends BaseDao implements UserDao { //根据SQL查询所有用户信息 public List getusersBySql(String sql,Object...args){ List list = new ArrayList(); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = getConn(); pstmt = conn.prepareStatement(sql); if(args!=null&&args.length>0){ for(int i = 0;irs = pstmt.executeQuery(); while(rs.next()){ User user = new User(); user.setUserno(rs.getInt("USERNO")); user.setUsername(rs.getString("USERNAME")); user.setSex(rs.getString("SEX")); user.setAge(rs.getInt("AGE")); user.setTellphone(rs.getString("TELLPHONE")); user.setAddress(rs.getString("ADDRESS")); user.setPermission(rs.getInt("permission")); user.setPassword(rs.getString("password")); list.add(user); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(rs, pstmt, conn); }return list; } //分页查询用户 public List getUsersByPager(Pager pager,User condition){ String conditionSql = ""; String sql = "select * from (select t1.*,rownum rn from userinfo t1 where rownum<=?"; if (condition.getUsername()!=null&&!condition.getUsername().trim().equals("")) { conditionSql="and username like '%"+condition.getUsername()+"%'"; } sql+=conditionSql; sql+=") where rn>?"; List list = getusersBySql(sql, pager.getCurrentPage()*pager.getPageSize(), (pager.getCurrentPage()-1)*pager.getPageSize()); String countSql = "select count(*) from userinfo where 1=1"+conditionSql; pager.setTotalCount(getUserCount(countSql)); return list; } //查询条数 public int getUserCount(String sql){ int count = 0; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = getConn(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ count = rs.getInt(1); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(rs, pstmt, conn); } return count; } //查询所有用户信息 public List getAllUsers(){ String sql = "select * from userinfo"; return getusersBySql(sql); } //根据用户名查询 public User getUserByUsernameandP(String username,String password){ String sql = "select * from userinfo where username = ? and password=?"; List list = getusersBySql(sql, username,password); return list != null && list.size()>0 ? list.get(0) : null; } //根据编号查询 public User getUserByUserno(int userno){ String sql = "select * from userinfo where userno = ?"; List list = getusersBySql(sql, userno); return list != null && list.size()>0 ? list.get(0) : null; } public List getUserByUserno1(int userno){ String sql = "select * from userinfo where userno = ?"; List list = getusersBySql(sql, userno); return list; } //增加 public int addUser(User user){ String sql = "insert into userinfo values(seq_user.nextval,?,?,?,?,?,?,?)"; return super.executeSql(sql,user.getUsername(),user.getSex(),user.getAge(),user.getTellphone(),user.getAddress(),user.getPermission(),user.getPassword()); } //修改 public int updateUser(User user){ String sql = "update userinfo set username=?,sex=?,age=?,tellphone=?,address=?,permission=? ,password=? where userno=?"; return super.executeSql(sql, user.getUsername(),user.getSex(),user.getAge(),user.getTellphone(),user.getAddress(),user.getPermission(),user.getPassword(),user.getUserno()); } //删除 public int deleteUser(String username){ String sql = "delete from userinfo where username=?"; return super.executeSql(sql, username); } public int deleteUser(int userno){ String sql = "delete from userinfo where userno=?"; return super.executeSql(sql, userno); } public int updatePassword(String password,String username){ String sql="update userinfo set password=? where username=?"; return super.executeSql(sql, password,username); } public User getUserByUsername(String username){ String sql="select * from userinfo where username=?"; List list=getusersBySql(sql, username); return list != null && list.size()>0 ? list.get(0) : null; } public List getUserByUsername1(String username){ String sql="select * from userinfo where username=?"; List list=getusersBySql(sql, username); return list; } }
fdspackage mypkg; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.sql.*; import java.sql.Statement; import javax.swing.table.*; import javax.swing.tree.*; /** * * @author Administrator */ public class ClassBrowse extends JFrame implements TreeSelectionListener { Statement stmt; ResultSet rs,rsl; DefaultTableModel dtm;//定义数据模型 String[]title;//表头数组 JTree tree;//树 JTable table; DefaultTreeModel treeModel = null;//定义一个默认的树模型,用于建立树 JScrollPane scroll1,scroll2;//定义两个滚动面板 JSplitPane splitPane;//定义一个分割面板 private Object con; public ClassBrowse(){ try{ stmt = ConnectServer1. con. createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("专业");//根 treeModel = new DefaultTreeModel(root);//建立树模型 initTree(root,0);//调用递归函数建立树模型 tree = new JTree(treeModel);//利用树模型建立树 scroll1 = new JScrollPane(tree); tree.addTreeSelectionListener(this); rs = stmt.executeQuery("select classclass.name as 班级,xsda.no as 学号,xsda.name as 姓名" + " from xsda,classclass where xsda.classid = classclass.id"); ResultSetMetaData dbmd = rs.getMetaData();//得到记录集的元数据,建立表头数组 title = new String[dbmd.getColumnCount()]; for(int i=1;i <=dbmd.getColumnCount();i++) title[i-1] = dbmd.getColumnName(i); dtm = new DefaultTableModel(null,title);//建立表格数据模型 table = new JTable(dtm);//利用数据模型建立表格 scroll2 =new JScrollPane(table); initTable();//建立表 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll1,scroll2); splitPane.setOneTouchExpandable(true); splitPane.setDividerLocation(200); this.getContentPane().add(splitPane,null);} catch(SQLException e){System.out.println(e);} setTitle("分类查询学生信息"); setSize(500,400); setVisible(true); } void initTree(DefaultMutableTreeNode root,int id){//这是递归函数 DefaultMutableTreeNode node = null;//定义节点 try{ Statement stmt = ConnectServer1. con. createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("select * from classclass where parent =" + id);//查询班级类表 while(rs.next()){ int nno = rs.getInt("id");//取得分类ID String nname = rs.getString("name").trim();//取得分类名称 node = new DefaultMutableTreeNode(nname);//建立树的节点 treeModel.insertNodeInto(node, root, root.getChildCount());//插入节点 initTree(node,nno);} } catch(Exception e){} finally{ try{ rs.close(); }catch(Exception e){} } } public void valueChanged(TreeSelectionEvent e){ JTree tree=(JTree)e.getSource(); //利用JTree的getLastSelectedPathCoponent()方法取得目前选取的节点 DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String name = ((String)selectionNode.getUserObject()); String sql = "selcet classclass.name as 班级,xsda.no as 学号,xsda.name as 姓名 from xsda,classclass where xsda.classid = classclass.id"; if(!name.equals("专业")) sql=sql + "and classid in(select id from classclass where rtrim(ltrim(str(id)))like(select ltrim(rtrim(str(id)))+'%'from classclass where rtrim(ltrim(name))='"+name+"'))"; try{ rs = stmt.executeQuery(sql);//利用嵌套查询查出所有数据 initTable(); } catch(Exception ee){} } void initTable(){ dtm.setRowCount(0); try{ rs.beforeFirst(); while(rs.next()){ Vector v1=new Vector(); for(int i=1;i <=title.length;i++) v1.addElement(rs.getString(i)); dtm.addRow(v1); } }catch(SQLException e){e.printStackTrace();} dtm.fireTableStructureChanged(); } public static void main(String arg[]){ JFrame.setDefaultLookAndFeelDecorated(true); Font font = new Font("JFrame",Font.PLAIN,14); Enumeration keys = UIManager.getLookAndFeelDefaults().keys(); while(keys.hasMoreElements()){ Object key = keys.nextElement(); if(UIManager.get(key)instanceof Font) UIManager.put(key, font); } if(!ConnectServer1.conn("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xsgl","sa","")){ JOptionPane.showMessageDialog(null, "连接数据库失败"); System.exit(0);//关闭数据库 } new ClassBrowse(); } }

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧