JSP+JDBC,数据库数据显示在前台的代码是什么
1个回答
展开全部
首先你要先创建一个类,用于连接的类:如下 conn.java
package conn;
import java.sql.*;
public class conn
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:conn";
private Connection conn=null;
private Statement stmt=null;
ResultSet rs=null;
public conn()
{
try
{
Class.forName(strDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("conn():"+e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
以下是前台JSP的代码
<jsp:useBean id="db1" scope="page" class="conn.conn"/>
<jsp:useBean id="db2" scope="page" class="conn.conn"/>
<form name="querydb" method="post" action="o_types.jsp" onsubmit="return isValid();" >
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#a8c7ce" >
<tr>
<td bgcolor="d3eaef" width="30" height="40"><div align="center"><span class="STYLE10">序号</span></div></td>
<td bgcolor="d3eaef" height="40"><div align="center"><span class="STYLE10">类别名称</span></div></td>
<td bgcolor="d3eaef" height="40"><div align="center"><span class="STYLE10">备注</span></div></td>
<td bgcolor="d3eaef" height="40"><div align="center"><span class="STYLE10">添加时间</span></div></td>
<td bgcolor="d3eaef" width="70" height="40"><div align="center"><span class="STYLE10">修改</span></div></td>
<td bgcolor="d3eaef" width="70"height="40"><div align="center"><span class="STYLE10">删除</span></div></td>
</tr>
<tr>
<%
String sql1="select * from types order by addtime ";
ResultSet rs2=db1.executeQuery(sql1);
String typename="";
String remark="";
String add_time="";
int rec_id=0;
int s=0;
while(rs2.next())
{
s++;
rec_id=rs2.getInt("rec_id");
typename=rs2.getString("typename");
remark=rs2.getString("remark");
add_time=rs2.getString("addtime");
if(remark.equals(""))
remark=" ";
%>
<TR>
<td bgcolor="#FFFFFF" align="center"><span class="STYLE10"><%=s%></span></td>
<TD bgcolor="#FFFFFF" align="center"><span class="STYLE10"><%=typename%></span></TD>
<TD bgcolor="#FFFFFF" align="center"><span class="STYLE10"><%=remark%></span></TD>
<TD bgcolor="#FFFFFF" align="center"><span class="STYLE10"><%=add_time.substring(0,19)%></span></TD>
<td bgcolor="#FFFFFF" align="center"><a href="javascript:detail(<%=rec_id%>);" class="ccc">修改</a></td>
<td bgcolor="#FFFFFF" align="center"><a HREF="javascript:del('<%=rec_id%>')">删除</a></td>
</TR>
<%
}
rs2.close();
%>
</table>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询