JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 透過jbdc搜尋資料庫的資料出現錯誤訊息
yanyonyuen





發文: 39
積分: 0
於 2004-01-13 23:18 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好
我透過jdbc來select資料庫的資料,卻出現下面的錯誤訊息
java.lang.Exception: An exception occured while retrieving Maintain.java.lang.NullPointerException

已經確認過資料庫確實有我要找的資料
我所使用的環境如下
資料庫mssql
os:win2000 server
jdk1.4.1
使用tomcat4.1.24

程式碼的片段如下
這是JSP的程式碼
request.setCharacterEncoding("Big5");
String[] classroom1 = request.getParameterValues("classroom");
String cr = classroom1[0];
Classroom.connect();
Classroom.viewvalue(cr);
ResultSet rs = Classroom.viewclassroom(); 測試過程是執行到此行就或出現上述的錯誤訊息

下面是JDBC的程式碼
public void viewvalue(String room){
Classroom.room = room;
}

public ResultSet viewclassroom() throws SQLException, Exception{
ResultSet rs = null;
try
{
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT classroom, cpNum1, cpNum2" +
"FROM Maintain " +
"where Maintain.classroom = '" + Classroom.room + "'" +
"and Maintain.statenum != 3 ");
}
catch (SQLException sqle)
{
error = "SQLException: Could not execute the query." + sqle.toString();
throw new SQLException(error);
}
catch (Exception e)
{
error = "An exception occured while retrieving Maintain." + e.toString();
throw new Exception(error);
}
return rs;
}

請教各位高手要如何解決


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
browser

戀香

版主

發文: 3570
積分: 1
於 2004-01-13 23:21 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
程式碼請先排版 ................

請先看清楚 論壇提問的智慧FAQ Smile


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
yanyonyuen





發文: 39
積分: 0
於 2004-01-13 23:39 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好
我透過jdbc來select資料庫的資料,卻出現下面的錯誤訊息
java.lang.Exception: An exception occured while retrieving Maintain.java.lang.NullPointerException

已經確認過資料庫確實有我要找的資料
我所使用的環境如下
資料庫mssql
os:win2000 server
jdk1.4.1
使用tomcat4.1.24

程式碼的片段如下
這是JSP的程式碼
1
2
3
4
5
6
  request.setCharacterEncoding("Big5");
  String[] classroom1 = request.getParameterValues("classroom");
  String cr = classroom1[0];
  Classroom.connect();
  Classroom.viewvalue(cr);
  ResultSet rs = Classroom.viewclassroom(); 


下面是JDBC的程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  public void viewvalue(String room){
    Classroom.room = room;
  }
 
  public ResultSet viewclassroom() throws SQLException, Exception{
    ResultSet rs = null;
    try
      {
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT classroom, cpNum1, cpNum2" +
                                           "FROM Maintain " +
                                           "where Maintain.classroom = '" + Classroom.room + "'" +
                                           "and Maintain.statenum != 3 ");
       }
     catch (SQLException sqle)
       {
         error = "SQLException: Could not execute the query." + sqle.toString();
         throw new SQLException(error);
        }
      catch (Exception e)
        {
          error = "An exception occured while retrieving Maintain." + e.toString();
          throw new Exception(error);
         }
       return rs;
         }
 

請教各位高手要如何解決


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
TAHO

可愛吧∼∼

版主

發文: 271
積分: 7
於 2004-01-14 00:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yanyonyuen wrote:
各位好
我透過jdbc來select資料庫的資料,卻出現下面的錯誤訊息
java.lang.Exception: An exception occured while retrieving Maintain.java.lang.NullPointerException

已經確認過資料庫確實有我要找的資料
我所使用的環境如下
資料庫mssql
os:win2000 server
jdk1.4.1
使用tomcat4.1.24

程式碼的片段如下
這是JSP的程式碼
1
2
3
4
5
6
  request.setCharacterEncoding("Big5");
  String[] classroom1 = request.getParameterValues("classroom");
  String cr = classroom1[0];
  Classroom.connect();
  Classroom.viewvalue(cr);
  ResultSet rs = Classroom.viewclassroom(); 



錯誤訊息中沒有告知是哪一行出現 exception 嗎?

我懷疑是你的 cr 沒抓到值∼∼
檢查一下 cr 是不是 null 吧∼∼


reply to postreply to post
請養成良好的 Java 程式碼慣例 習慣
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:TAHO]
yanyonyuen





發文: 39
積分: 0
於 2004-01-14 10:50 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yanyonyuen wrote:
各位好
我透過jdbc來select資料庫的資料,卻出現下面的錯誤訊息
java.lang.Exception: An exception occured while retrieving Maintain.java.lang.NullPointerException

已經確認過資料庫確實有我要找的資料
我所使用的環境如下
資料庫mssql
os:win2000 server
jdk1.4.1
使用tomcat4.1.24

程式碼的片段如下
這是JSP的程式碼

1
2
3
4
5
6
7
 
request.setCharacterEncoding("Big5");  
String[] classroom1 = request.getParameterValues("classroom");  
String cr = classroom1[0];  經過測試該行程式有抓到值
Classroom.connect(); 
Classroom.viewvalue(cr);  
ResultSet rs = Classroom.viewclassroom(); 此行出現錯誤訊息


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-14 11:07 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

程式碼的片段如下
這是JSP的程式碼

1
2
3
4
5
6
7
 
request.setCharacterEncoding("Big5");  
String[] classroom1 = request.getParameterValues("classroom");  
String cr = classroom1[0];  經過測試該行程式有抓到值
Classroom.connect(); 
Classroom.viewvalue(cr);  
ResultSet rs = Classroom.viewclassroom(); 此行出現錯誤訊息



Classroom是一個物件還是類別? 我猜是物件,因為您列出的viewvalue(cr)不是static函式
假設Classroom是物件,那麼您是怎麼生成這個物件的??

建議您給出完整程式,這樣真的很難以判斷,尤其您使用Database的方法是用自定函式


liminjun edited on 2004-01-14 11:09
reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:liminjun]
browser

戀香

版主

發文: 3570
積分: 1
於 2004-01-14 11:11 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
liminjun wrote:
建議您給出完整程式,這樣真的很難以判斷,尤其您使用Database的方法是用自定函式


對呀 .. 很多人都以為這裡的人很強 ...
可以不用看 code 就知道那裡錯了 ....

不過如果他說
1
String cr = classroom1[0];
有取到值
那就肯定是
1
2
Classroom.viewvalue(cr);  
ResultSet rs = Classroom.viewclassroom();

出錯呀 ...
可是誰曉得他的 Classroom 是什麼東西 ??


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:liminjun]
yanyonyuen





發文: 39
積分: 0
於 2004-01-14 11:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是整個完整的程式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<%@ page contentType = "text/html; charset=big5" %>
<%@ page import = "java.sql.* , java.io.* , java.util.*" errorPage = "error.jsp" %>
 
<jsp:useBean id = "Classroom" class = "com.database.Classroom"/>
 
<html>
  <head>
    <title>Classroom</title>
  </head>
  <body>
 
    <form name = "myForm1">
       <%
         request.setCharacterEncoding("Big5");
         String[] classroom1 = request.getParameterValues("classroom");
         String cr = classroom1[0];
       %>
        
        <caption><font face = "標楷體" ; size = 8 ><b>您想要查詢<%= cr %>損壞的電腦如下</b></font></caption>
    
        <%
           Classroom.connect();
           Classroom.viewvalue(cr);
           ResultSet rs = Classroom.viewclassroom();
        
           while (rs.next()) {
  String classroom;
  String cpNum1;
  String cpNum2;
 
                classroom = rs.getString("classroom");
       if(rs.wasNull()) {
           classroom = null;
       }
 
                cpNum1 = rs.getString("cpNum1");
        if(rs.wasNull()) {
           cpNum1 = null;
        }
 
                cpNum2 = rs.getString("cpNum2");
         if(rs.wasNull()) {
            cpNum2 = null;
         }
 
             %>
 
<caption><font face = "標楷體" ; size = 4><b><%= cpNum1 %><%= cpNum2 %></b></font></caption><br>
 
              <% } %>
 
              <% Classroom.disconnect(); %>
<caption><font face = "標楷體" ; size = 6 ><b>請同學上課時不要使用這些有問題的電腦,謝謝!!</b></font></caption>
  </form>
</body>
</html>  


下面是jdbc的程式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.database;
 
import java.sql.*;
import java.util.*;
 
public class Classroom{
 
    String error;
    static String room;
    Connection con;
    static{
       try{
           String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
           Class.forName(driver);
       }
       catch(Exception e){
               e.printStackTrace();
       }
     }
    
  public Classroom() {}
 
    //建立與資料庫作連線及註冊JDBC驅動程式
 
  public void connect() throws SQLException,Exception {
     try {
          Connection con = DriverManager.getConnection("jdbc:Microsoft:sqlserver://must-project:1433;DatabaseName=SCHOOL","sa","must");
     }
     catch(SQLException cnfe){
          error = "SQLException: Could not connect to database. " + cnfe.toString();
          throw new SQLException(error);
      }
      catch(Exception e){
           error = "Exception: An unknown error occurred while connecting to database. " + e.toString();
           throw new Exception(error);
       }
    }
 
//終止資料庫連線
 
  public void disconnect() throws SQLException {
     try{
         if(con != null){
          con.close();
          }    
     }
     catch(SQLException sqle) {
           error = "SQLException: Unable to close the database connection. " + sqle.toString();
           throw new SQLException(error);
      }
    }
 
  public void viewvalue(String room){
 
    Classroom.room = room;
 
    }
 
    public ResultSet viewclassroom() throws SQLException, Exception{
       ResultSet rs = null;
       try
       {
          Statement stmt = con.createStatement();
          rs = stmt.executeQuery("SELECT classroom, cpNum1, cpNum2" +
                                             "FROM Maintain " +
                                             "where Maintain.classroom = '" + Classroom.room + "'" +
                                             "and Maintain.statenum != 3 ");
      }
      catch (SQLException sqle)
      {
         error = "SQLException: Could not execute the query." + sqle.toString();
         throw new SQLException(error);
      }
      catch (Exception e)
      {
          error = "An exception occured while retrieving Maintain." + e.toString();
          throw new Exception(error);
       }
      return rs;
       }
}


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-14 11:54 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我猜問題出在於

1
    static String room;

1
2
3
       rs = stmt.executeQuery("SELECT classroom, cpNum1, cpNum2"
 +"FROM Maintain " + "where Maintain.classroom = '" +
 Classroom.room  + "'" +"and Maintain.statenum != 3 ");

1
  public void viewvalue(String room){    Classroom.room = room;    }


老實說,真的不懂您為何會這樣用,在此使用static變數不是會造成同步問題嗎?


liminjun edited on 2004-01-14 12:00
reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:liminjun]
browser

戀香

版主

發文: 3570
積分: 1
於 2004-01-14 12:06 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果直接使用
1
String room;
.....
會不會簡單一些呀 .... Smile

為什麼你會想要用 static 呢 ??


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
sdargon





發文: 109
積分: 0
於 2004-01-14 12:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我覺得會錯是因為con沒設
程式中有兩個Connection con的宣告
而viewclassroom()裡頭用的con未初始化


sdargon edited on 2004-01-14 12:42
reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:sdargon]
liminjun

JavaDOCHelper真讚



發文: 496
積分: 5
於 2004-01-14 14:01 user profilesend a private message to usersend email to liminjunreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sdargon wrote:
我覺得會錯是因為con沒設
程式中有兩個Connection con的宣告
而viewclassroom()裡頭用的con未初始化


仔細看了一下,的確是這樣
抓別人程式的臭蟲...真累啊 辛苦您了


reply to postreply to post
作者 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 [Re:yanyonyuen]
Millerlai





發文: 56
積分: 1
於 2004-01-14 23:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yanyonyuen wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    //建立與資料庫作連線及註冊JDBC驅動程式
 
  public void connect() throws SQLException,Exception {
     try {
      
     con = DriverManager.getConnection("jdbc:Microsoft:sqls
     erver://must-project:1433;DatabaseName=SCHOOL","sa","must");

     }
     catch(SQLException cnfe){
          error = "SQLException: Could not connect to database. " + cnfe.toString();
          throw new SQLException(error);
      }
      catch(Exception e){
           error = "Exception: An unknown error occurred while connecting to database. " + e.toString();
           throw new Exception(error);
       }
    }



改成這樣就行了....

不然你的Classroom類別的field ==> con要幹麻的??

另外,你的
<jsp:useBean id = "Classroom" class = "com.database.Classroom"/> 這個也不太好

id又跟類別名稱同名 雖然不影響功能面,但是會影響程式可讀性


reply to postreply to post
» JWorld@TW »  JDBC/SQL討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8