1.jsp
<%@ page contenttype="text/html;charset=gb2312" language="java" import="java.sql.*" %>
<%resultset rs;
connection conn;
statement stmt;
string sql="";
string username="db_username";
string userpwd="db_password";
string servername="db_server";
string dbname="database_name";
string drivername = "org.gjt.mm.mysql.driver";
class.forname(drivername).newinstance();
string url = "jdbc:mysql://" + servername + "/" + dbname;
conn = drivermanager.getconnection(url, username, userpwd);
stmt = conn.createstatement();
sql = "select * from userinfo";
rs = stmt.executequery(sql);
%>
<body>
<%while(rs.next()){%>
<p>
<%=rs.getstring("username")%>
</p>
<%}%>
<body>
这样也能连数据库,但是会让别人在源文件中看到自己的数据来源,不安全,不推荐使用。