小弟有两个问题想请教:  
问题一、我写了一个类config.java用来初始化数据库连接,其中有一个方法  
getStatement(),用来返回一个Statement实例,  
我又写了一个dealServlet,在其中的使用  
Statement  statement  =  new  config().getStatement();想得到Statement的实例  
然后判断  
if(statement  ==  null)  {  
out.println("<html><head></head><body>"+"create  statement  worng?"+"</body></html>");  
 }  
下面是对statement操作,使用数据库,(使用dealServlet?action=view&id=5调用doGet()方法)  
但在浏览器里返回的是  <html><head></head><body>create  statement  worng?</body></html>这样的代码,于是我在config.java里写了个方法public  String  getText(){  return  "ok";}  
在dealServlet里调用后,浏览器里结果是提示XML  parse  error!内容里ok(Servlet返回类型是xml)  
我想请教一个为什么statement就无法初始化呢?  
 
问题二、同样使用上面说的两个类,我使用Servlet?action=add调用doPost()方法,传入一个自己构造的一个XML形式的字符串,在doPost()体里使用  
private  String  readXMLFromRequestBody(HttpServletRequest  request){  
StringBuffer  xml  =  new  StringBuffer();  
String  line  =  null;  
 
try{  
BufferedReader  reader  =  request.getReader();  
while((line  =  reader.readLine())  !=  null)  {  
xml.append(line);  
}  
}  
catch(Exception  e){  
System.out.println("Error  reading  XML:  "  +  e.toString());  
}  
return  xml.toString();  
}  
再用String  temps  =  readXMLFromRequestBody(request);  
out.println(temps);打印结果,却不是希望的传入的那个XML字符串,这是为什么呢,怎么解决?

解决方案 »

  1.   

    config.java我是这样写的
    import java.sql.*;
    import com.mysql.jdbc.Driver;
    import java.util.Properties;
    import java.lang.String;
    import java.io.FileInputStream;public class config {
    //the var
    private String driverName = "";
    private String userName = "";
    private String userPassword = "";
    private String DBName = "";
    private Connection connection = null;
    private  Statement statement = null;
    //the construct
    public config(){
    try{
    //read the var's value form the properties file
    Properties argument = new Properties();
    argument.load(new FileInputStream("connect.properties"));
    driverName = argument.getProperty("driverName");
    userName = argument.getProperty("userName");
    userPassword = argument.getProperty("userPassword");
    DBName = argument.getProperty("DBName");
    //creat the link string
    String url="jdbc:mysql://localhost/"+DBName+"?user="+userName+"&password="+userPassword+"&useUnicode=true&characterEncoding=gb2312";
           //create a new connection and statement
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           connection=DriverManager.getConnection(url);
           statement = connection.createStatement();
           }catch(SQLException sqle){
            sqle.printStackTrace();
           }catch(Exception e){
            e.printStackTrace();
           }
    }
    //return a statement
    public Statement getStatement(){
    return this.statement;
    }
           //for test
    /*public String getText(){
    return "okok";
    }*/
    //test function use the main
    /*public static void main(String[] args){
    config con = new config();
    System.out.println(con.getStatement().toString());
    }*/
    }
      

  2.   

    我在dealServlet里写了个main方法试了一下可以查询数据库,但在浏览器里就会出问题的,
    这是怎么回事啊,
    是不是要有特别的设置什么的呢?
      

  3.   

    Statement  statement  =  new  config().getStatement();
    这条语句应改为Statement  statement  = config().getStatement();
      

  4.   

    mysql的包有放到common\lib里吗
    你在初始化的时候向控制台输出得到的statement看看
      

  5.   

    回genzhongzhang() ,
    ----------
    我改成那样后,编译无法通过提示找不到config回vacuumboy(菜鸟依旧) 
    我在dealServlet里的main是这样写的
    public  static void main(String[] args){
       try{
       Statement statement = new config().getStatement();
       System.out.println(statement.toString());
         //select all the wishes form the datebase
                  String query = "select * from wishes";
      //get all the wishes and create response xml file
               StringBuffer temps = new StringBuffer();
                      ResultSet it=statement.executeQuery(query);
                      while(it.next()){
                       temps .append(it.getString("id"));
                       temps.append("\n");
                       temps.append(it.getString("name"));
                       }
                       System.out.println(temps.toString());
                       }
                       catch(Exception ee){
       ee.printStackTrace();
       }
    结果是:
    com.mysql.jdbc.Statement@b6ece5
    1
    purple_spring这样的结果是不是对的呢?
      

  6.   

    我又发现个问题,我怕是config这个名字有问题我改成了configer.java
    但在main()函数里不管我写
    Statement statement = new config().getStatement()
    还是Statement statement = new configer().getStatement()
    得到的结果是一样的,但改成其它的就不行了,
    是不是这有点问题呢?
      

  7.   

    测试不要只在main函数里测试,在WEB环境下运行的结果可能会有所不一样
    就在构造函数里输出
      

  8.   

    你的意思是在configer.java的构造函数里输出啊,
    好,我试试
      

  9.   

    我在构造函数里加了System.out.println(statement.toString());
    又把public String getText(){
         return statement.toString();
    }
    在dealServlet里的main函数里结里不变,
    但在web环境下提示
    HTTP Status 500 -type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionjava.lang.NullPointerException
    violet.ajax.configer.getText(configer.java:45)
    violet.ajax.deal_WishServlet.doGet(deal_WishServlet.java:23)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.到底要如何做才能正确使用呢?
      

  10.   

    在tomcat的log里提示:java.io.FileNotFoundException: connect.properties (系统找不到指定的文件。)
    那connect.properties文件要放在哪里呢,是不是在web环境里找不到本地的那文件呢
      

  11.   

    那就找到原因了,没有找到配置文件
    connect.properties文件应该是放在config.class所在的目录,实在不行的话
    argument.load(new FileInputStream("connect.properties"));里的文件名加上绝对路径,应该就可以了。
      

  12.   

    我是放在class文件下的,但不行,
    你说的是绝对路径?
    那要怎么写呢,
    谢谢,请指点一下,
    我的是E:\Tomcat 5.5\webapps\youyuan\WEB-INF\classes,是不是这样写呢
      

  13.   

    properties文件放在容器的工作目录下
      

  14.   

    绝对路径就是E:\Tomcat 5.5\webapps\youyuan\WEB-INF\classes\connect.properties
      

  15.   

    youyuan应该算是我的工作目录了吧,
    我直接放在youyuan下一个,和.java文件同个目录下一个,和.class文件同个目录下一个,
    都不行啊
      

  16.   

    你可以放在E:\Tomcat 5.5\webapps\youyuan目录下试看看
      

  17.   

    谢谢vacuumboy(菜鸟依旧) 
    我改成了绝对路径后搞定了,那第二个问题是怎么回事呢?
      

  18.   

    我不知道怎么说了,
    我把大体的代码说出来吧,我是初学AJAX,
    我是用javascript构造一个xml流,作为post体的,
    var name = document.getElementById("name").value;
     var address = document.getElementById("formTocall").value;
     var flag = "ok";
     var temp = document.getElementById("allow");
    var wish = document.getElementById("wish").value;
    xml = "<wishes>" + "<name>"+name+"<\/type>"+"<formTocall>"+address+"<\/formTocall>"+"<flag>"+flag+"<\/flag>"+"<wish>"+wish+"<\/wish>"+ "<\/wishes>";
    createXMLHttpRequest();
    xmlHttp.open("POST",url,true);
    xmlHttp.onreadystatechange = handleback;
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlHttp.send(xml);dealServlet里的doPost()里
    String xml = readXMLFromRequestBody(request);
    Document xmlDoc = null;
    try {
        xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
    }
    catch(ParserConfigurationException e) {
    System.out.println("ParserConfigurationException:  "+e);
    }catch(SAXException ea){
     System.out.println("SAXException:  " + ea);
     }
     
     //get the value of paramers's name from the request xml file
             String name = xmlDoc.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
          String formTocall =xmlDoc.getElementsByTagName("formTocall").item(0).getFirstChild().getNodeValue();
             String flag = xmlDoc.getElementsByTagName("flag").item(0).getFirstChild().getNodeValue();    
         String wish = xmlDoc.getElementsByTagName("wish").item(0).getFirstChild().getNodeValue();
         String date = xmlDoc.getElementsByTagName("date").item(0).getFirstChild().getNodeValue();
    但我在html文件里输入信息由js文件发送里,
    得到tomcat的返回信息是在500,
    提示在String name = xmlDoc.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
    这一句得到了NullPointerException还有第一问题如果换个目录的话,还要重新写一下绝对路径,再编译啊,
    有没有自动获得路径的函数啊?
      

  19.   

    应该是相对路径才对,不然解析xml文件时有问题
      

  20.   

    xml = "<wishes>" + "<name>"+name+"<\/type>"+"<formTocall>"+address+"<\/formTocall>"+"<flag>"+flag+"<\/flag>"+"<wish>"+wish+"<\/wish>"+ "<\/wishes>";
    name两边的标签没有对称
      

  21.   

    呵呵,
    这个是我写错了,我已经解决了,
    现在有的是,第一问题,
    我想问一下,有没有只写个相对路径让机器自动找到文件的方法呢,我写了个URL类也不行啊,
    只有写了绝对路径才能得到结果的,
    但那不是换个路径就要重新编译啊
      

  22.   

    你可以把工作目录print出来看看(用System的一个Property,具体记不住了)是哪里,然后放到这个目录下面
      

  23.   

    将你的connect.properties文件放在类加载路径里.WEB-INF/classes目录下.或者用绝对路径.