永发信息网

请帮我注释下这段代码(每行注释下)!谢谢!

答案:2  悬赏:50  手机版
解决时间 2021-08-01 19:18
  • 提问者网友:咪咪
  • 2021-08-01 09:56
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%!Connection conn = null;
Statement st = null;
ResultSet rt = null;%>

<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student1",
"sa", "");
st = conn.createStatement();
rt = st.executeQuery("select * from student");
while (rt.next()) {
%>


<table>

<tr>
<td><%=rt.getInt(1)%>
</td>
<td><%=rt.getString("sname")%></td>
<td><%=rt.getString("ssex")%></td>
<td><%=rt.getString("sage")%></td>
<td><%=rt.getString("sgrade")%></td>

</tr>


</table>

<% }


conn.close();
st.close();
rt.close();
%>




</body>
</html>

最佳答案
  • 五星知识达人网友:野味小生
  • 2021-08-01 11:13

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> //这个是加载的选项,pageEncode是字符编码
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();//得到页面上下文
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort() //得到当前路径
+ path + "/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">


<title>My JSP 'MyJsp.jsp' starting page</title>


<meta http-equiv="pragma" content="no-cache"> //各种配置信息,比如前两个就是没有缓存
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">//加载CSS层叠样式
-->


</head>


<body>
<%!Connection conn = null; //定义变量
Statement st = null;
ResultSet rt = null;%>


<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//加载数据库驱动
conn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student1",
"sa", ""); //链接数据库
st = conn.createStatement();
rt = st.executeQuery("select * from student");//得到statement后就可以执行SQL语句了,这句就是得到student表中的所有记录
while (rt.next()) { //循环输出所得记录,只要有记录就进行
%>



<table>
//以表格实现记录的显示
<tr>
<td><%=rt.getInt(1)%>
</td>
<td><%=rt.getString("sname")%></td>
<td><%=rt.getString("ssex")%></td>
<td><%=rt.getString("sage")%></td>
<td><%=rt.getString("sgrade")%></td>


</tr>



</table>


<% }



conn.close();//循环完毕之后要关闭数据库连接,状态,和recordset,释放资源
st.close();
rt.close();
%>




</body>
</html>

全部回答
  • 1楼网友:行雁书
  • 2021-08-01 12:18

这是一段查询数据库的jsp代码

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn = DriverManager .getConnection( "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=student1", "sa", ""); st = conn.createStatement(); rt = st.executeQuery("select * from student");

这个是连接数据库后进行查询

while (rt.next()) { %> <table> <tr> <td><%=rt.getInt(1)%> </td> <td><%=rt.getString("sname")%></td> <td><%=rt.getString("ssex")%></td> <td><%=rt.getString("sage")%></td> <td><%=rt.getString("sgrade")%></td> </tr> 这个是将查询结果,循环输出在一个table里

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯