我在插入查询中创建了一个 select -where 子查询,但我的数据库中没有可见的更新。
我需要首先从数据库中选择一个值,然后从数据库中传递该所选值,然后在另一个查询中传递该所选值以从 mysql 数据库中获取另一个值。
这是我的示例代码
<%@ page import="javax.servlet.http.HttpSession" %>
<%@ page import="java.util.*,java.sql.*" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<%@page import="com.dbutil.CrudOperation"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/fullview.css">
<script src="jquery.js"></script>
<script src="bootstrap.min.js"></script>
<title>Job Details</title>
</head>
<body>
<% Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
HttpSession hs = null;
con = CrudOperation.createConnection();
hs = request.getSession();
String value=request.getParameter("idJob");
String id = (String) hs.getAttribute("userinfo");
%>
<div class="container">
<%String strsql = "select name from register where uname=?";
try
{
ps=con.prepareStatement(strsql);
ps.setString(1, id);
int rw1=ps.executeUpdate();
if(rw1>0) {
con.commit();
String tryname=rs.getString("name");
String strsql1 = "insert into postjob1(applicants) values(?) where id=?";
try
{
ps=con.prepareStatement(strsql1);
ps.setString(1, tryname);
ps.setString(2,value);
rs=ps.executeQuery();
if(rs.next()) {
con.commit();
response.sendRedirect("confirm.jsp");
}
else{
response.sendRedirect("Oops.jsp");}
}
catch(SQLException se)
{
System.out.println(se);
}
}
else{
response.sendRedirect("Oops1.jsp");}
}
catch(SQLException se)
{
System.out.println(se);
}
%>
%>
</div>
</body>
</html>
最佳答案
Insert 语句在表中创建新记录,并且没有 where 子句。因此,您的 SQL 中有语法错误。
关于java - 在 mysql 中的 INSERT-WHERE 查询中嵌套 SELECT-WHERE 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36236886/