不建议在JSP页面中直接写语句连接数据库
类别: JSP教程
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>
这样也能连数据库,但是会让别人在源文件中看到自己的数据来源,不安全,不推荐使用。
<%@ 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>
这样也能连数据库,但是会让别人在源文件中看到自己的数据来源,不安全,不推荐使用。
- 上一篇: JSP 对 XML 的操作
- 下一篇: 用正则表达式判断一个简单的用户登陆的例子
-= 资 源 教 程 =-
文 章 搜 索