简体   繁体   中英

I am try to insert data in mysql table

I want to insert data in 2 table with single query. bt it gives error

mycode is shown below.

public class Trains extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        int tid=Integer.parseInt(request.getParameter("tid"));
        String tname=request.getParameter("tname");
        String ttype=request.getParameter("ttype");
        String stncode=request.getParameter("stn_code");
        String stnname=request.getParameter("stn_name");
        String availtime=request.getParameter("Avail_time");
        String depttime=request.getParameter("Dep_time");
        String halttime=request.getParameter("Halt_time");
        String dist=request.getParameter("distance");

        String[] avail_day=request.getParameterValues("week");

        try {
            String query="INSERT INTO Train_info(Train_ID,Train_Name,Train_Type,Runs_On) values(?,?,?,?); INSERT INTO Train_route(stn_code,stn_name,arrival_time,Destn_time,Halt_time,Distance) values(?,?,?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Railway","root","mysql123");
            PreparedStatement ps=con.prepareStatement(query);

            ps.setInt(1, tid);
            ps.setString(2, tname);
            ps.setString(3, ttype);
            ps.setString(4, stncode);
            ps.setString(5, stnname);
            ps.setString(6, availtime);
            ps.setString(7, depttime);
            ps.setString(8, halttime);
            ps.setString(9, dist);
            for(int i=0;i<avail_day.length;i++){
            ps.setString(10, avail_day[i]);
            }

            int i=ps.executeUpdate();
            if(i>0){
                out.println("Successfully Registration");
                response.sendRedirect("TrainRoute.jsp");
            }
            else{
                out.println("Registration failed");
                response.sendRedirect("Error.java");
            }
        }
        catch(ClassNotFoundException ce){
            ce.printStackTrace();
        }
        catch(Exception e){
            out.println(e);
        }
    }
}

error is:-

java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO Train_route(stn_code,stn_name,arrival_time,Destn_time,Halt_time,Dist' at line 1

See this: Java - Mysql - Multiple query

The INSERT INTO your syntax error message calls out the second insert in your query string. The MySQL JDBC driver, like most MySQL drivers, only accepts one query per call to Connection.preparedStatement() call.

You'll need to refactor your code to use two prepared statements, once for each table.

If you're used to working with other makes and model of SQL table servers, this restriction can trip you up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM