使用JDBC

时间:2016-06-21 08:47:45

标签: java mysql jdbc

我的数据库中有两个表(filedetails和filestatus)。

我使用以下代码在filedetails表中插入值。

filestatus表中我有3列

filenumber,
fdepartment,
status(either in or out).

在我的status.jsp页面中,我提供了3个下拉列表,其中2个下拉列表从filenumber表中取departmentfiledetails,1取得状态(在IN或OUT)。

如果状态为OUT,则只需在filestatus表中插入值,但如果值已插入filestatus,则department对应的filenumber会更新。< / p>

问题是filenumber,如果我使用以下代码插入filenumber那么我将如何更新文件编号?

还有其他方法吗?

file.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert File Page</title>
    <style>
header {
background-color:teal;
color:white;
text-align:center;
padding:30px;
}

section {
width:350px;
float:left;
padding:150px;
}
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
   <%
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("user")) userName = cookie.getValue();
}
}
%>
<header>
<h3>Hi <%=userName %></h3>
</header>
<a href="create.jsp"><font color="black">back</font></a>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<section>
<form action="FileServlet" method="post">
<h3>Insert File Details</h3>
    <table border="1">
        <tbody>
            <tr>
                <td>File Name :</td>
                <td><input type="text" name="filename" value="" size="50"    /></td>
            </tr>
            <tr>
                <td>File Type</td>
                <td><input type="text" name="type" value="" size="50" />  </td>
            </tr>
            <tr>
                <td>Place of Origin(company) :</td>
                <td><input type="text" name="company" value="" size="50"   /></td>
            </tr>
            <tr>
                <td>HeadOffice :</td>
                <td><input type="text" name="HO" value="" size="50" /> </td>
            </tr>
            <tr>
                <td>File Location :</td>
                <td><input type="text" name="department" value="" size="50" />  </td>
            </tr>
            <tr>
                <td>Subject :</td>
                <td><input type="text" name="subject" value="" size="50"   /></td>
            </tr>
            <tr>
                <td>File Number :</td>
                <td><input type="text" name="fileno" value="" size="50"   /></td>
            </tr>

        </tbody>
    </table>
    <input type="reset" value="Clear" name="Clear" />
    <input type="submit" value="Submit" name="Submit" />

</form>
</section>
<footer>
Copyright 2008 NSIC. All right reserved.   
</footer>
</body>
</html>

fileServlet.java

package bean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FileServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
   Cookie[] cookies = request.getCookies();
    if(cookies != null){
    for(Cookie cookie : cookies){
        if(cookie.getName().equals("JSESSIONID")){
            System.out.println("JSESSIONID="+cookie.getValue());
            break;
        }
    }
    }       
    HttpSession session = request.getSession(false);
    System.out.println("User="+session.getAttribute("user"));
   if(session!=null && session.getAttribute("user") != null){
                String user=(String)session.getAttribute("user"); 

          boolean status=false;
    try{            
        String fname=request.getParameter("name");            
        String type=request.getParameter("type");
        String company=request.getParameter("department");
        String headoffice=request.getParameter("HO"); 
        String location=request.getParameter("department");
        String subject=request.getParameter("subject");            
        String fno=company+"/"+headoffice+"/"+location+"/"+type+"/"+fname;


        Connection con=ConnectionProvider.getCon();
        String sql="insert into files(fileno,fname,ftype,subject,company,headoffice,fdepartment) values (?,?,?,?,?,?)";
        PreparedStatement pstmt =con.prepareStatement(sql);

        pstmt.setString(1,fno); 
        pstmt.setString(2,fname);
        pstmt.setString(3,type);
        pstmt.setString(4,subject); 
        pstmt.setString(5,company);
        pstmt.setString(6,headoffice);
        pstmt.setString(7,location);

        int rs=pstmt.executeUpdate();
        if(rs>0){status=true;}
    }catch(Exception e){System.out.println(e);}
    if(status){
               PrintWriter out= response.getWriter();
               out.println("Values have been inserted"+user);
               response.sendRedirect("create1.jsp");

    }
               else 
              {
                  PrintWriter out= response.getWriter();
                  out.println("failed");
                  response.sendRedirect("create1.jsp");

              }                                
    }else{
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.html");
    PrintWriter out= response.getWriter();
    out.println("<font color=red>Either user name or password is wrong.</font>");
    rd.include(request, response);
    }
}
}

create.jsp提供了为filedetails.jsp或status.jsp选择的链接

status.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>File Status Page</title>
     <style>
header {
background-color:teal;
color:white;
text-align:center;
padding:30px;
}

section {
width:350px;
float:left;
padding:150px;
}
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
     <%
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("user")) userName = cookie.getValue();
}
}
%>
<header>
<h3>Hi <%=userName %></h3>
</header>
<a href="create.jsp"><font color="black">back</font></a>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<section>
<h3>Change Status</h3>
<form action="statusServlet" method="post">
        <select name="files">
                <%
    try{
String sql="select * from files";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
        "root", "root");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
%>                          
  <option value="<%=rs.getInt("fileno")%>"><%=rs.getString("fname")%></option>
<%}
rs.close();
st.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%> 
        </select>
        <select name="departments">
            <%
    try{
String sql="select * from department";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
        "root", "root");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
%>                          
  <option value="<%=rs.getInt("departmentid")%>"><%=rs.getString("departmentname")%></option>
<%}
rs.close();
st.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
        </select>
        <select name="input">
            <option>IN</option>
            <option>OUT</option>
        </select>
        <input type="submit" value="submit" name="submit" />
    </form>
</section>
<footer>
Copyright 2008 NSIC. All right reserved.                             
</footer>
</body>
</html>

我还没有创建statusServlet.java

1 个答案:

答案 0 :(得分:0)

我会将部门分成两部分,如下所示。

1)发起文件的部门

2)文件的当前位置。

我将使用文件来源部门生成func mapViewDidFinishLoadingMap(mapView: MGLMapView) { // your code here } 并将单独更新当前位置(最初当前位置将与原始部门相同)。

如果有人比这更有效,那么我也会接受。