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 DatabaseFollowing 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());
}
%>
执行结果: