Topic: ResultSet返回为null? |
Print this page |
1.ResultSet返回为null? | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-04 14:21 <jsp:useBean id="sqlb" scope="page" class="packages.util.DataBase" /> <%@ page import="java.sql.*;" %> <% try{ String str2="select * from ta"; ResultSet rs=sqlb.executeSearch(str2); if(rs==null) out.print("空"); while(rs.next()) out.println(rs.getInt("id")+"\t"+rs.getString("name")); rs.close(); sqlb.closeStmt(); sqlb.closeConn(); }catch(Exception e){ out.println(e.getMessage()); } %> 为什么在jsp中ResultSet 返回为 空 呢 packages.util.DataBase在应用程序中测试是好的啊 //******************************************************** package packages.util; import java.sql.*; public class DataBase { String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=./data/demo.mdb"; Connection conn=null; Statement stmt=null; ResultSet rs=null; String user=null; String password=null; public DataBase(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Connection conn=DriverManager.getConnection(strurl,"","") ; //Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); }catch(java.lang.ClassNotFoundException cnfe){ System.err.println(cnfe); } } public void executeChange(String sqlStr){//记录更新,插入,删除 try{ conn=DriverManager.getConnection(strurl,user,password); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate(sqlStr); stmt.close(); conn.close(); }catch(SQLException ex){ System.err.println("sql_data.executeUpdate:"+ex.getMessage()); } } public ResultSet executeSearch(String sqlStr){//记录查询 try{ conn=DriverManager.getConnection(strurl,user,password); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=stmt.executeQuery(sqlStr); }catch(SQLException ex){ System.err.println("sql_data.executeQuery:"+ex.getMessage()); } return rs; } public void closeStmt(){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } } public void closeConn(){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } 为什么在jsp中ResultSet 返回为空呢 packages.util.DataBase在应用程序中测试是好的啊 是不是demo.mdb路径放错了? 我放在 myapp/data/demo.mdb 下 myapp下是test.jsp |
2.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: Jcat Posted on: 2006-04-04 15:01 我也曾经试图想在JSP中访问Access,同学告诉我是因为Windows的权限机制在作怪,折腾了半天的结果是,我可以通过JSP在Access中:select from,create table,drop table……就是不能 insert into,郁闷死我了。 |
3.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-04 15:15 真有这样的缘由? 可是我现在连select 都没啊 |
4.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: Jcat Posted on: 2006-04-04 17:13 那先看看你的数据源有没有设置正确? |
5.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-04 17:49 packages.util.DataBase在应用程序中测试是好的啊 数据源应该没问题吧 上面源程序那样,不用配置系统数据源的 |
6.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: chy305 Posted on: 2006-04-04 22:10 MDB的路径改为绝对路径试试 |
7.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: Jcat Posted on: 2006-04-05 08:19 在windows下设置权限的办法 (缩略图,点击图片链接看原图) |
8.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-05 08:51 MDB的路径改为绝对路径 一样没效果阿 |
9.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-05 08:53 不关权限的问题吧,权限是全部开放的。 |
10.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: tomcatexpert Posted on: 2006-04-11 18:16 I tried ur code using absolute path. It works fine. did u pay extra attention on folder separators? neil99 wrote: |
11.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-15 10:14 谢谢 能告诉我各个文件存放的位置吗(包括数据库文件) |
12.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-15 10:28 能用相对路径吗 |
13.Re:ResultSet返回为null? [Re: neil99] | Copy to clipboard |
Posted by: neil99 Posted on: 2006-04-15 10:48 在jsp里 好象这里写相对路径是错的 得是绝对路径才行 要重起服务器, 错误已经解决了 但为什么java应用程序里就行呢 找到一个专门来获取绝对路径的javaBean(path_test.java) 来解决问题 //############################# package pathtest; import java.io.*; import javax.servlet.*; import javax.servlet.jsp.PageContext;//导入PageContext类,不要忘了 public class path_test { protected ServletContext m_application; private boolean m_denyPhysicalPath; public path_test() { } public final void initialize(PageContext pageContext) throws ServletException { m_application = pageContext.getServletContext(); } public String getPhysicalPath(String filePathName, int option) throws IOException { String path = new String(); String fileName = new String(); String fileSeparator = new String(); boolean isPhysical = false; fileSeparator=System.getProperty("file.separator"); if(filePathName == null) throw new IllegalArgumentException("There is no specified destination file (1140)."); if(filePathName.equals("")) throw new IllegalArgumentException("There is no specified destination file (1140)."); if(filePathName.lastIndexOf("\\") >= 0) { path = filePathName.substring(0, filePathName.lastIndexOf("\\")); fileName = filePathName.substring(filePathName.lastIndexOf("\\") + 1); } if(filePathName.lastIndexOf("/") >= 0) { path = filePathName.substring(0, filePathName.lastIndexOf("/")); fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1); } path = path.length() != 0 ? path : "/"; java.io.File physicalPath = new java.io.File(path); if(physicalPath.exists()) isPhysical = true; if(option == 0) { if(isVirtual(path)) { path = m_application.getRealPath(path); if(path.endsWith(fileSeparator)) path = path + fileName; else path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName)); return path; } if(isPhysical) { if(m_denyPhysicalPath) throw new IllegalArgumentException("Physical path is denied (1125)."); else return filePathName; } else { throw new IllegalArgumentException("This path does not exist (1135)."); } } if(option == 1) { if(isVirtual(path)) { path = m_application.getRealPath(path); if(path.endsWith(fileSeparator)) path = path + fileName; else path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName)); return path; } if(isPhysical) throw new IllegalArgumentException("The path is not a virtual path."); else throw new IllegalArgumentException("This path does not exist (1135)."); } if(option == 2) { if(isPhysical) if(m_denyPhysicalPath) throw new IllegalArgumentException("Physical path is denied (1125)."); else return filePathName; if(isVirtual(path)) throw new IllegalArgumentException("The path is not a physical path."); else throw new IllegalArgumentException("This path does not exist (1135)."); } else { return null; } } private boolean isVirtual(String pathName) //判断是否是虚拟路径 { if(m_application.getRealPath(pathName) != null) { java.io.File virtualFile = new java.io.File(m_application.getRealPath(pathName)); return virtualFile.exists(); } else { return false; } } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |