JSP题:使用JDBC访问数据库表user_table, 输出表中全部记录。
展开全部
list_emp.jsp
举个例子,大概是这样。将例子里的emp改成user_table就可以了
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head><title></title></head>
<body>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/test" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "root" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
String sql = "SELECT empno,ename,job,sal,hiredate FROM emp" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇佣日期</td>
</tr>
<% int count=0;
while(rs.next()&&count<2){
count++;
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
float sal = rs.getFloat(4) ;
java.util.Date date = rs.getDate(5) ;
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=date%></td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch(Exception e) {
System.out.println(e) ;
}finally{
rs.close() ;
pstmt.close() ;
conn.close() ; // 如果直接关闭连接也可以
}
%>
</body>
</html>
举个例子,大概是这样。将例子里的emp改成user_table就可以了
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head><title></title></head>
<body>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/test" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "root" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
String sql = "SELECT empno,ename,job,sal,hiredate FROM emp" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇佣日期</td>
</tr>
<% int count=0;
while(rs.next()&&count<2){
count++;
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
float sal = rs.getFloat(4) ;
java.util.Date date = rs.getDate(5) ;
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=date%></td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch(Exception e) {
System.out.println(e) ;
}finally{
rs.close() ;
pstmt.close() ;
conn.close() ; // 如果直接关闭连接也可以
}
%>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询