m4_demo_how_to_use_bean.jsp
<%@ page language="java" contentType="text/html; charset=BIG5" pageEncoding="BIG5"%>
Insert title here
<% tu.JustAmethod("select * from products "); %>
<% if (tu.成功) { %>
<%= "成功" %>
<% if (tu.幾列 == 0) { %>
沒有資料
<% } else {
String[] each_row; %>
有資料
<% for (int 第幾筆 = 0; 第幾筆 < tu.幾列; 第幾筆 += 1) {
each_row = tu.rows.elementAt(第幾筆); %>
<% for (int 第幾欄 = 0; 第幾欄 < tu.幾欄; 第幾欄 += 1) {
String column;
column = each_row[第幾欄]; %>
<%=column%>
<% } %>
<% } %>
<% } %>
<% } else { %>
<%= "失敗原因:" + tu.失敗原因 %>
<% } %>
my_javabeans.java
package com;
import java.sql.*;
import java.util.*;
public class my_javabeans {
public int 幾欄;
public int 幾列;
public Vector rows;
public String 失敗原因;
public boolean 成功;
public void JustAmethod(String query) {
java.sql.Connection conn = null;
Statement cmd = null;
ResultSet dr = null;
try {
失敗原因 = "";
成功 = false;
rows = new Vector();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = java.sql.DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databasename=northwind",
"sa", "III");
cmd = conn.createStatement();
dr = cmd.executeQuery(query);
ResultSetMetaData rsmd = dr.getMetaData();
幾欄 = rsmd.getColumnCount();
String [] each_row ;
幾列 = 0;
while ( dr.next() ) {
幾列 += 1;
each_row = new String[幾欄];
for(int cn = 1; cn <= 幾欄 ; cn += 1)
{
each_row[cn-1] = dr.getString(cn);
}
rows.addElement(each_row);
}
成功 = true;
} catch (java.sql.SQLException exp) {
失敗原因 = "資料庫 problem: " + exp.getMessage();
} catch (ClassNotFoundException exp) {
失敗原因 = "jdbc driver problem: " + exp.getMessage();
}
}
}
沒有留言:
張貼留言