gpt4 book ai didi

java - 使用 JDBC 在 mysql 表中插入和更新

转载 作者:行者123 更新时间:2023-11-29 20:52:16 26 4
gpt4 key购买 nike

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

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

filestatus表中我有3列

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

在我的 status.jsp 页面中,我给出了 3 个下拉列表,其中 2 个下拉列表采用 filenumberdepartment 来自 filedetails table 和 1 接受状态(IN 或 OUT)。

如果状态为 OUT,则值将简单地插入到 filestatus 表中,但如果状态为 OUT,则值将插入到对应于的 filestatusdepartmentfilenumber 已更新。

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

还有其他方法可以做到这一点吗?

文件.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 的链接

状态.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) 文件的来源部门

2) 文件的当前位置。

我将使用文件原始部门来生成文件编号,并单独更新当前位置(最初当前位置将与原始部门相同)。

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

关于java - 使用 JDBC 在 mysql 表中插入和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com