query.jsp:
<%
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.JDBC.SQLServerDriver");//加载Microsoft公司的JDBC驱动程序
String strConn="JDBC:sqlserver://LIUZC\\SQLEXPRESS:1433;DatabaseName=a";//设置连接字符串
String strUser="sa";
String strPassword="liuzc518";
conn=DriverManager.getConnection(strConn,strUser,strPassword);//创建连接对象
Statement stmt=conn.createStatement();//使用conn.createStement()创建stement对象
String strSql="SELECT TOP 5 p_id,p_type,p_name,p_price,p_quantity FROM product order by p_time desc";
ResultSet rs=stmt.executeQuery(strSql);//通过Statement对象的executeQuery()方法,执行指定的查询并将结果集保存在rs中
%>
<center><h2>最新前5位商品信息</h2></center>
<table border="1" align="center">
<tr>
<td>商品编号</td>
<td>商品类别</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品数量</td>
</tr>
<%while (rs.next()){%>//利用while循环结构输出rs中的值
<tr bgcolor="lightblue">
<td><%=rs.getString("p_id")%></td>
<td><%=rs.getString("p_type")%></td>
<td><%=rs.getString("p_name")%></td>
<td><%=rs.getFloat("p_price")%></td>
<td><%=rs.getInt("p_quantity")%></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e)
{
out.println(e.getMessage());
}
catch(SQLException e)
{
out.println(e.getMessage());
}
%>
</table>
executeUpdate():一般用于执行SQL 的insert、update、delete、也可以执行SQL数据定义语言如create、alter、drop等.返回影响的记录总行数。
executeQuery():一般用于执行SQL的select。返回ResultSet接口实例(即结果集)
execute():一般在不知道执行SQL语句后会产生什么结果或可能有多种类型的结果产生时才会使用。返回ResultSet结果集和影响的记录总行数。
<%
Connection conn=null;
try{
Class.forName("com.microsoft.sqlserver.JDBC.SQLServerDriver");//加载Microsoft公司的JDBC驱动程序
String strConn="JDBC:sqlserver://LIUZC\\SQLEXPRESS:1433;DatabaseName=a";//设置连接字符串
String strUser="sa";
String strPassword="liuzc518";
conn=DriverManager.getConnection(strConn,strUser,strPassword);//创建连接对象
Statement stmt=conn.createStatement();//使用conn.createStement()创建stement对象
String strSql="SELECT TOP 5 p_id,p_type,p_name,p_price,p_quantity FROM product order by p_time desc";
ResultSet rs=stmt.executeQuery(strSql);//通过Statement对象的executeQuery()方法,执行指定的查询并将结果集保存在rs中
%>
<center><h2>最新前5位商品信息</h2></center>
<table border="1" align="center">
<tr>
<td>商品编号</td>
<td>商品类别</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品数量</td>
</tr>
<%while (rs.next()){%>//利用while循环结构输出rs中的值
<tr bgcolor="lightblue">
<td><%=rs.getString("p_id")%></td>
<td><%=rs.getString("p_type")%></td>
<td><%=rs.getString("p_name")%></td>
<td><%=rs.getFloat("p_price")%></td>
<td><%=rs.getInt("p_quantity")%></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e)
{
out.println(e.getMessage());
}
catch(SQLException e)
{
out.println(e.getMessage());
}
%>
</table>
executeUpdate():一般用于执行SQL 的insert、update、delete、也可以执行SQL数据定义语言如create、alter、drop等.返回影响的记录总行数。
executeQuery():一般用于执行SQL的select。返回ResultSet接口实例(即结果集)
execute():一般在不知道执行SQL语句后会产生什么结果或可能有多种类型的结果产生时才会使用。返回ResultSet结果集和影响的记录总行数。