Dec 2, 2008

JSP and MySQL

所需的JDBC Driver下載處
MySQL: 請按此
將jdbc Driver放至適當目錄位置(一般為{站台/WEB-INF/lib}或{tomcat}/common/lib)

----------------- index.jsp ----------------------
<%@ page contentType="text/html; charset=Big5"
import="java.sql.*"%>
<HTML>
<HEAD>
<TITLE>ResultSet變數的建立與記錄的取得</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>ResultSet變數的建立與記錄的取得</FONT>
</CENTER>
<HR>
<CENTER>
<%
/*以下的程式請將要使用的驅動程式及建立連線語法的註解符號去除
* 建立資料庫連線,其中
* localhost代表本機(或以IP表示,如192.168.1.1)
* 3306代表MySQL所使用的網路port,不同的資料庫用不同的port,請依事實設定
* pubs3代表所連結的資料庫名稱
* useUnicode=true&characterEncoding=big5是設定傳值時所用的編碼系統為"big5"
* root為資料庫的使用者,其他資料庫使用者為sa
* 123456為密碼
*/
//載入JDBC驅動程式類別

//第一個 利用JDBC for MySQL 連結MySQL資料庫(複製mysql-connector-java-3.x.x-bin.jar,其中x代表版本)
//Class.forName("com.mysql.jdbc.Driver");
//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs3?useUnicode=true&characterEncoding=big5","root", "123456");

//第二個 利用Sybase jconnect 6.0 連結Sybase資料庫,(複製jconnect6/jconn3.jar)
//Class.forName("com.sybase.jdbcx.SybDriver");
//Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:192.168.1.1:5000/pubs3?CHARSET=big5", "sa", "123456");

//第二個 利用Sybase jconnect 5.5 連結Sybase資料庫,(複製jconnect5_5/jconn2.jar)
//Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
//Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:192.168.1.1:5000/pubs3?CHARSET=big5", "sa", "123456");

//第三個 利用sourceforge提供的jtds.jdbc連結Sybase資料庫
// 亦可用此jdbc driver連結 MS-SQL資料庫,因為兩者核心是相同的(複製jtds-x.x.jar,其中x是代表版本)
//Class.forName("net.sourceforge.jtds.jdbc.Driver");
//以下這行是連到MS-SQL
//Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.1:1433/pubs","sa","123456");
//以下這行是連到SYBASE
//Connection conn = DriverManager.getConnection("jdbc:jtds:sybase://192.168.1.1:5000/pubs3;CHARSET=big5","sa","123456");

//以下的程式碼都相同
//建立Statement物件
Statement stmt = conn.createStatement();

//建立ResultSet(結果集)物件
ResultSet rs;

//執行SQL敘述
rs = stmt.executeQuery("SELECT * FROM titles");

%>
<!-- 將搜尋出來的資料以表格的方式呈現 (本行是HTML的註解)-->
<TABLE bgcolor=DodgerBlue>
<TR bgcolor=SkyBlue>
<TD><B>title_id</B></TD>
<TD><B>title</B></TD>
<TD><B>type</B></TD>
</TR>
<%
//利用while迴圈將資料表中的記錄列出
while (rs.next())
{
%>
<TR bgcolor=LightGoldenrodYellow>
<TD><B><%= rs.getString("title_id") %></B></TD>
<TD><B><%= rs.getString("title") %></B></TD>
<TD><B><%= rs.getString("type") %></B></TD>
</TR>
<%
}
rs.close(); //關閉ResultSet物件
stmt.close(); //關閉Statement物件
conn.close(); //關閉Connection物件
%>
</TABLE>
</CENTER>
</BODY>
</HTML>
------------- End index.htm ---------------------

遭遇 MySQL 回傳 error 時
java.sql.SQLException: null, message from server: "Host '192.168.x.x' is not allowed to connect to this MySQL server"


--- Solution
1)By default mysql does not support remote access...

2) you will have to specify this in the server first, (of course you will not be able to do this from a remote machine!!! unless you have remote desktop access)

3)To enable access from a client machine called nile, do the following after starting mysqld (or mysqld_safe) on the server, named delaware:

% mysql -u root -p
mysql> use mysql;
mysql> grant all privileges on *.* to usrname1@'%' identified by 'test' with grant option;
mysql> exit

The user name, usrname1, and the password, test, should be replaced by the real value.
One can then run mysql on nile as follows:

% mysql --host=delaware --user=usrname1 -ptest

To restrict access to a user from a specific host, change '%' in the grant command to a valid hostname or ip address.

0 意見: