JSP版本的数据库操作

代码时间:2015-6-16

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.ge[color=darkred][/color]tScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
String _id = request.getParameter("id");
String _name = request.getParameter("name");
String _mail = request.getParameter("mail");
String sql = null;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP '1.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 bgcolor="#FOFFFF">
  <h3>查询数据库</h3>
  <table border="1px" width="250px" hight="200px" bgcolor="#7FFFD4">
  <form name="form1" method="get">
  <tr>
  <td>ID:</td>
  <td><input type="text" name="id" ><br></td>
  <!--  <input type="submit" name="searchID" value="ID查询"><br> -->
 
  <td>NAME:</td>
  <td><input type="text" name="name" ><br></td>
  <!-- <input type="submit" name="searchNAME" value="NAME查询"><br> -->
 
  <td>MAIL:</td>
  <td><input type="text" name="mail" ><br></td>
  </tr>
  <!-- <input type="submit" name="searchMAIL" value="MAIL查询"><br> -->
  <tr>
  <td colspan="2"><input type="submit" name="search" value="查询"><br></td>
  </tr>
 
  </form>
   </table>
   <h3>操作数据库</h3>
<table border="1px" width="250px" hight="200px" bgcolor="#7FFFD4">
  <form name="form2" action="11.jsp" method="get">
  <tr>
  <td>ID:</td>
  <td><input type="text" name="id" ><br></td>
  <!--  <input type="submit" name="searchID" value="ID查询"><br> -->

  <td>NAME:</td>
  <td><input type="text" name="name" ><br></td>
  <!-- <input type="submit" name="searchNAME" value="NAME查询"><br> -->
  <td>MAIL:</td>
  <td><input type="text" name="mail" ><br></td>
  <!-- <input type="submit" name="searchMAIL" value="MAIL查询"><br> -->
  </tr>
  <tr>
  <td colspan="3"><input type="submit" name="operation" value="add">
  <input type="submit" name="operation" value="delect">
  <input type="submit" name="operation" value="update"></td>
  </tr>
  </form>
</table>
<%
  sql="select * from test1 where";
 
  if (_id != "")
sql="select * from test1 where id = "+_id;
else if (_name != "")
sql="select * from test1 where name = '"+_name+"'";
else if(_mail != "")
sql="select * from test1 where mail = '"+_mail+"'";
else
sql="select * from test1";

//out.println(sql);
%>
<br>
  <%
  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  Class.forName("org.mariadb.jdbc.Driver").newInstance(); //用于加载Driver类(jdbc驱动器),,,registerDriver注册java.sql.DriverManager.registerDriver
String url = "jdbc:mariadb://192.168.1.100:3306/test?user=root&password=pdcss";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

    %>
 
  <table border="1px" width="700px" bgcolor="#7FFFD4">
     <font color = "red">
    <%
    while(rs.next()){
    out.print("<tr>");
    out.print("<td>"+rs.getInt(1)+"&nbsp;&nbsp;</td>");
    out.print("<td>"+rs.getString(2)+"&nbsp;&nbsp;</td>");
    out.print("<td>"+rs.getString(3)+"&nbsp;&nbsp;</td>");
    out.print("</tr>");
    }
    %>
    </font>
    </table>
    <%
    if(rs != null){
    rs.close();
    }
    if(stmt != null){
    stmt.close();
    }
    if(conn != null){
    conn.close();
    }
     %>
  </body>
</html>

 

posted @ 2017-07-15 10:27  乐在克里特  阅读(80)  评论(0编辑  收藏  举报