Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 ResultSet返回为null?
neil99





发贴: 19
积分: 0
于 2006-04-04 14:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
<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



作者 Re:ResultSet返回为null? [Re:neil99]
Jcat

熊猫的猫



发贴: 266
积分: 16
于 2006-04-04 15:01 user profilesend a private message to usersend email to Jcatsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也曾经试图想在JSP中访问Access,同学告诉我是因为Windows的权限机制在作怪,折腾了半天的结果是,我可以通过JSP在Access中:select from,create table,drop table……就是不能 insert into,郁闷死我了。


编程,游泳,睡觉--SleepingCat
作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-04 15:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真有这样的缘由?
可是我现在连select 都没啊



作者 Re:ResultSet返回为null? [Re:neil99]
Jcat

熊猫的猫



发贴: 266
积分: 16
于 2006-04-04 17:13 user profilesend a private message to usersend email to Jcatsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那先看看你的数据源有没有设置正确?


作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-04 17:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
packages.util.DataBase在应用程序中测试是好的啊
数据源应该没问题吧
上面源程序那样,不用配置系统数据源的



作者 Re:ResultSet返回为null? [Re:neil99]
chy305





发贴: 2
积分: 0
于 2006-04-04 22:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
MDB的路径改为绝对路径试试


作者 Re:ResultSet返回为null? [Re:neil99]
Jcat

熊猫的猫



发贴: 266
积分: 16
于 2006-04-05 08:19 user profilesend a private message to usersend email to Jcatsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在windows下设置权限的办法

(缩略图,点击图片链接看原图)



作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-05 08:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
MDB的路径改为绝对路径
一样没效果阿



作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-05 08:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不关权限的问题吧,权限是全部开放的。


作者 Re:ResultSet返回为null? [Re:neil99]
tomcatexpert





发贴: 52
积分: 2
于 2006-04-11 18:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I tried ur code using absolute path.
It works fine.

did u pay extra attention on folder separators?

neil99 wrote:
MDB的路径改为绝对路径
一样没效果阿



作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-15 10:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢

能告诉我各个文件存放的位置吗(包括数据库文件)



作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-15 10:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
能用相对路径吗


作者 Re:ResultSet返回为null? [Re:neil99]
neil99





发贴: 19
积分: 0
于 2006-04-15 10:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在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;
}
}

}




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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