天天看點

linux jsp mysql_Linux JSP連接配接MySQL資料庫

Linux(Ubuntu平台)JSP通過JDBC連接配接MySQL資料庫,與Windows平台類似,步驟如下:

解壓 jdbc: tar -zxvf mysql-connector-java-5.1.18.tar.gz

配置 jdbc:cp mysql-connector-java-5.1.18-bin.jar

/usr/local/jdk1.6.0_22/jre/lib/ext/

JSP示例:

Read from mySQL Database

Following records are selected from table "gametop800"
top id name country dtime

String DRIVER = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost:3306/top800";

Connection con = null;

ResultSet rst = null;

Statement stmt = null;

int i = 1;

try {

Class.forName(DRIVER).newInstance();

con = DriverManager.getConnection(url, "root", "");

stmt = con.createStatement();

rst = stmt.executeQuery("select top, id, name, country, dtime from gametop800 where top=1");

while (rst.next()) {

if (0 == i%2) {

%>

.

%>

.  

i++;

}

rst.close();

stmt.close();

con.close();

} catch (Exception e) {

System.out.println(e.getMessage());

}

%>

執行結果:

linux jsp mysql_Linux JSP連接配接MySQL資料庫