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

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 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
話題樹型展開
人氣 標題 作者 字數 發文時間
2847 透過jbdc搜尋資料庫的資料出現錯誤訊息 yanyonyuen 1398 2004-01-13 23:18
2434 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 browser 165 2004-01-13 23:21
2618 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 yanyonyuen 1481 2004-01-13 23:39
2533 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 TAHO 615 2004-01-14 00:10
2674 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 yanyonyuen 537 2004-01-14 10:50
2491 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 liminjun 450 2004-01-14 11:07
2358 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 browser 321 2004-01-14 11:11
2941 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 yanyonyuen 3875 2004-01-14 11:34
2486 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 Millerlai 852 2004-01-14 23:09
2525 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 liminjun 430 2004-01-14 11:54
2311 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 browser 82 2004-01-14 12:06
2340 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 sdargon 67 2004-01-14 12:38
2540 Re:透過jbdc搜尋資料庫的資料出現錯誤訊息 liminjun 145 2004-01-14 14:01
» JWorld@TW »  JDBC/SQL討論區

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8