数据库中的数据怎么显示到网页中
数据库中的数据在网页显示的代码如下:
<?php$sql = new mysqli("localhost","用户名","密码","数据库");
if(mysqli_connect_errno()){echo "cnnect_errno";exit;}$query = "select * from student"; $result = $sql->query($query);$num_results = $result->num_rows;//总页数
$cpage = isset($_GET["page"]) ? $_GET["page"] :1; //当前页$num = 10; //每页的页数$url = "fen_ye.php";//每次请求你的页面$offset=($cpage-1) * $num;//数据读取的位置$pagenum = ceil($num_results/$num);//总页数$query = "select * from student limit {$offset},{$num}";//取数据$result = $sql->query($query);$num_results = $result->num_rows;//每一页的的开始与结尾$start = $offset + 1; $end=($cpage==$pagenum)? $num_results : ($cpage*$num); // 上一页 和 下一页$next=($cpage==$pagenum)? 0 : ($cpage+1);//如果页面到了结尾 就返回0 就是不在下一页了$prev=($cpage==1) ? 0 : ($cpage - 1);for($i = 0; $i<$num_results;$i++){$row = $result->fetch_assoc();echo "<table align='center' width='200' border='1' >";echo "<tr align=left width='100'>";echo "<td>{$row["cno"]}</td>"; echo "<td>{$row["name"]}</td>"; echo "<td>{$row["age"]}</td>"; echo "<td>{$row["sex"]}</td>"; echo "</tr>";echo "</table>"; }echo "<table align=center backgrouand='red'>";echo "<tr>";echo "<td>当前页总数:{$num_results}</td>";echo "<td>本页条数:{$start}-{$end}</td>";echo "<td>页面位置:{$cpage}/{$pagenum}</td>";if($cpage == 1){echo "<td>首页</td>";}else{echo "<td><a href='{$url}?page=1'>首页</a>;</td>";}if($prev){echo "<td><a href ='{$url}?page={$prev}'>上一页</a></td>";}else{echo "<td>上一页</td>";}if($next){echo "<td><a href='{$url}?page={$next}'>下一页</td>";}else{echo "<td>下一页</td>";}if($cpage == $pagenum){echo "<td>尾页</td>";}else{echo "<td><a href='{$url}?page={$pagenum}'>尾页</td>";}
echo "</tr>";echo "</table>";?>
2024-09-02 广告
你要有一个jsp页面,你可以查下jsp怎么连接对应数据库
编写连接数据库的程序,以下是jsp连接oracle程序
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<HTML><BODY>
<hr align="center" width="55%" color="#991111" size="5"/>
<%
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("GBK");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@localhost:1521:orcl";
conn=DriverManager.getConnection(url,"scott","tiger");
stmt=conn.prepareStatement("select * from emp");
rs=stmt.executeQuery();
out.print("<TABLE BORDER='1' width='55%' cellpadding='3' align=center>");
out.print("<TR><TD>"+"<b>Empno</b>"+"</TD>");
out.print("<TD>"+"<b>Ename</b>"+"</TD>");
out.print("<TD>"+"<b>Job</b>"+"</TD>");
out.print("<TD>"+"<b>Mgr</b>"+"</TD>");
out.print("<TD>"+"<b>hiredate</b>"+"</TD>");
out.print("<TD align=center>"+"<b>Sal</b>"+"</TD>");
out.print("<TD>"+"<b>Comm</b>"+"</TD>");
out.print("<TD>"+"<b>Deptno</b>"+"</TD></TR>");
while(rs.next()){
out.print("<TR><TD>"+rs.getString(1)+"</TD>");
out.print("<TD>"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getString(3)+"</TD>");
out.print("<TD>"+rs.getString(4)+"</TD>");
out.print("<TD>"+rs.getString(5)+"</TD>");
out.print("<TD>"+rs.getString(6)+"</TD>");
out.print("<TD>"+rs.getString(7)+"</TD>");
out.print("<TD>"+rs.getString(8)+"</TD></TR>");
}
out.print("</TABLE>");
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
%>
</BODY></HTML>
可以考虑使用成熟的B/S开发技术框架,如
PHP : 经典且开源的技术
JSP :基于Java语言
ASP.Net : 需要C#语言支持
只是显示到页面的话只起到呈现数据的效果,写一个web页面,和API调用查询方法传到页面遍历出来就行了