Linux(Ubuntu平台)JSP通过JDBC连接MySQL数据库,与Windows平台类似,步骤如下:
解压jdbc:tar-zxvfmysql-connector-java-5.1.18.tar.gz
配置jdbc:cpmysql-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"
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());
}
%>
执行结果: