JSP连接mysql数据库例程:
<%@page contentType="text/html;charset=gb2312"%>
<%
//声明
java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
//regiester jdbc driver
String DBUser="数据库用户名";
String DBPassword="数据库密码";
String DBServer="localhost或者127.0.0.1";
String DBNAME="数据库名";
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//connect to the database
sqlConn=java.sql.DriverManager.getConnection
("jdbc:mysql://"+DBServer+":3306/"+DBNAME+"?useUnicode=true&characterEncoding=gb2312", DBUser,DBPassword);
//create the driver handle
sqlStmt=sqlConn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//excute sql
//sqlRst=sqlStmt.executeQuery("select * from test");
%>
<html>
<head>
<title>Linux-JSP-JDBC-MYSQL TESTING -SELETCT copyright <a href="mailto: postmaster@yourdomain.com "> postmaster@yourdomain.com </a> </title>
</head>
<body>
<table border="1" cellspace ="0" align="center">
<tr><td>DB CONNECT OK , </td></tr>
<tr>
<th>title </th>
<th>type </th>
</tr>
<%// while(sqlRst.next()) {%>
<tr>
<td> <%//=sqlRst.getString(1)%></td>
<td><%//=sqlRst.getString(2)%></td>
</tr>
<%//}%>
</table>
</body>
</html>
<%
//sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>