将数据从一个表插入另一个表时出错

时间:2022-09-15 22:28:01

When i use this code i get message "EROARE" from else clause else {out.println("EROARE");

当我使用这个代码时,我从else子句{out.println(“EROARE”)获得消息“EROARE”;

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
     <%@ page
        import="java.sql.*,java.util.*,models.*,database.*,controllers.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Inregistrare</title>
    </head>
    <body>

    <%
        String user = (String)session.getAttribute("Nick"); 
        String Serie = request.getParameter("Serie");

        Class.forName("com.mysql.jdbc.Driver");

                    try {
                       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppauto","root", "k474lin BLASIC");
                       Statement st = con.createStatement();
                       //  ResultSet rs;
                       int i = st.executeUpdate("insert into favorite(IDfav, Producator, Model, Proprietar, An_fabricatie, Combustibil, Culoare, Detalii, User) SELECT Serie, ProducatorM, ModelM, ProprietarM, An_fabricatie, Combustibil, Culoare, Detalii,'"+user+"' from masina where Serie='" + Serie  + "'");
                       if (i > 0) {
                         //   session.setAttribute("Nick", Nick);
                         response.sendRedirect("succes.jsp");
                         // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
                         } else {
                                 out.println("EROARE");              
                         }
                    }
                    catch (Exception ex) {


                             }

    %>
    </body>
    </html>

When i change line

当我换线

int i = st.executeUpdate("insert into favorite(IDfav, Producator, Model, Proprietar, An_fabricatie, Combustibil, Culoare, Detalii, User) SELECT Serie, ProducatorM, ModelM, ProprietarM, An_fabricatie, Combustibil, Culoare, Detalii,'"+user+"' from masina where Serie='" + Serie  + "'");

to

int i = st.executeUpdate("insert into favorite(IDfav, Producator, Model, Proprietar, An_fabricatie, Combustibil, Culoare, Detalii, User) SELECT Serie, ProducatorM, ModelM, ProprietarM, An_fabricatie, Combustibil, Culoare, Detalii,'"+user+"' from masina where Serie='17'");

(or any number from DATABASE) it works..

(或DATABASE中的任何数字)它的工作原理..

My question: how can i get data from table masina column Serie and use it, because every number from Serie represent a car and i what to do a favorite page, so i need the number from serie to add car to favorite list... I don't know if you guys understand what i need, hope you do.

我的问题:如何从表masina列Serie获取数据并使用它,因为来自Serie的每个数字代表一辆汽车而我该做什么最喜欢的页面,所以我需要来自serie的数字来添加汽车到收藏列表...我不知道你们是否理解我需要的东西,希望你能做到。

Here's is cautare.jsp, where i have "Favorite button"

这是cautare.jsp,我有“收藏夹”

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page
    import="java.sql.*,java.util.*,models.*,database.*,controllers.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
<title>Cauta</title>
</head>
<body>
    <div id="content"></div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="50%">&nbsp;</td>
            <td width="766" valign="top">
                <table width="766" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td colspan="2" class="top" height="69" valign="top">
                            <div class="menu">
                                <a href="indexadmin.jsp">Acasa</a><a href="cautare.jsp">Cauta</a><a href="userCP.jsp">Control Panel</a><a href="logout.jsp">Deconectare</a>
                            </div>
                        </td>
                    </tr>                                                                                                               
                    <tr>
                        <td>
                            <h2>Cauta masina</h2>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <form id="filter" method="post">
                                <label>Producator: </label>
                                <select name="ProducatorM" id="ProducatorM">
                                    <option value="0">Producator</option>

                                </select>

                            &nbsp;  

                                <input type="submit" name="cauta" value="Cauta"/>
                            </form>
                        </td>
                    </tr>                                                                                                                                                                                                                                                                                                                       
                    <tr>                                        
                        <td>
                        <% if (request.getParameter("cauta") != null) {
                            Masini mc = new Masini();

                            String p = request.getParameter("ProducatorM");
                            String m = request.getParameter("ModelM");
                            String a = request.getParameter("An_fabricatie");
                            String c = request.getParameter("Combustibil");
                            String S = request.getParameter("Serie");


                            Vector <Masina> items = mc.filter(p, m, a, c, S);       
                            Masina m1 = null;

                            Iterator<Masina> vItr = items.iterator(); 
                            System.out.println("items:" + items.size());                                                        

                            %>                                                                                                                                                                                                                      
                            <h3 style="margin-top:10px"> Rezultatele cautarii</h3>
                            <table style="margin-top:10px">
                            <% 

                            while(vItr.hasNext()) {
                                m1 = vItr.next();

                            %>                          
                                <tr>
                                    <td valign="top" style="background:#aaa; padding:5px 10px" width="102"><img width="125" height="80" src="images/cars/<%= m1.getPoza() %>" alt="" /></td>
                                    <td width="600px" valign="top" style="background:#eee; padding:5px 10px">
                                    <div style="font-weight:700; color:#000; font-size:14px">
                                    <b>
                                    <%= m1.getProducatorM() %> 
                                    <%= m1.getModelM() %> 
                                    <%= m1.getAn_fabricatie() %>
                                    <input type="hidden" name="Serie" value="<%=m1.getSerie()%>"> 
                                    <%= m1.getCombustibil() %></b>
                                    </div>
                                    <div style="font-size:13px; color:#000; margin-top:10px">
                                    <%= m1.getDetalii() %>


                    <html lang="en">
    <head>
        <title>Rate</title>
        <link rel="stylesheet" href="jquery.rating.css">
        <script src="jquery.js"></script>
        <script src="jquery.rating.js"></script>
    </head>
    <body>
        <form>
            <input type="radio" name="rating" value="1" class="star">
            <input type="radio" name="rating" value="2" class="star">
            <input type="radio" name="rating" value="3" class="star">
            <input type="radio" name="rating" value="4" class="star">
            <input type="radio" name="rating" value="5" class="star">


        </form>
       &nbsp;&nbsp; <a href="addtofav.jsp">Favorite</a>

    </body>

</html>             



                                    </div>                                                                                                                  
                                    </td>
                                </tr>
                                <% } %>
                            </table>
                            <% } %>
                        </td>
                    </tr>
                </table>
            </td>
          <td width="50%">&nbsp;</td>
        </tr>                                       
    </table>    
</body>
</html>

And here is class

这是课堂

package controllers;

import java.sql.ResultSet;
import java.sql.SQLException;
//import java.util.ArrayList;
import java.util.Vector;

import models.Masina;
import database.Config;
import database.Database;

public class Masini {

    private Database ppauto;

    public Masini() {
        super();
        ppauto = new Database(Config.driverName, Config.connURL, Config.userName,
                Config.password);
        //conexiunea la baza de date
        ppauto.DBManager();
        // db.SetConnection();
    }

    public String getDenumireP(String DenumireP) {
        //selecteaza producatorul cu id-ul denumirep
        String prod = "";
        String selectQuery = "select * from producator where DenumireP = " + DenumireP;
        ResultSet rs1 = ppauto.getResultSet(selectQuery);

        try {
            if (rs1.next()) {
                prod = rs1.getString("Tara");
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return prod;
    }

    public String getDenumire(String Denumire) {
        //selecteaza modelul cu id-ul denumire
        String mod = "";
        String selectQuery = "select * from model where Denumire = " + Denumire;
        ResultSet rs2 = ppauto.getResultSet(selectQuery);

        try {
            if (rs2.next()) {
                mod = rs2.getString("Caroserie");
                }   
            if (rs2.next()) {
                mod = rs2.getString("Producator");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mod;
    }

    public String getNick(String Nick) {
        //selecteaza proprietarul cu id-ul nick
        String prop = "";
        String selectQuery = "select * from proprietar where Nick = " + Nick;
        ResultSet rs3 = ppauto.getResultSet(selectQuery);

        try {
            if (rs3.next()) {
                prop = rs3.getString("Password");
            }
            if (rs3.next()) {
                prop = rs3.getString("Nume");
            }
            if (rs3.next()) {
                prop = rs3.getString("Prenume");
            }
            if (rs3.next()) {
                prop = rs3.getString("Telefon");
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return prop;
    }

    public String getSerie(int Serie) {
        //selecteaza masina cu id-ul serie
        String mas = "";
        String selectQuery = "select * from masina where Serie = " + Serie;
        ResultSet rs4 = ppauto.getResultSet(selectQuery);

        try {
            if (rs4.next()) {
                mas = rs4.getString("ProducatorM");
            }
            if (rs4.next()) {
                mas = rs4.getString("ModelM");
            }
            if (rs4.next()) {
                mas = rs4.getString("ProprietarM");
            }
            if (rs4.next()) {
                mas = rs4.getString("An_fabricatie");
            }
            if (rs4.next()) {
                mas = rs4.getString("Combustibil");
            }
            if (rs4.next()) {
                mas = rs4.getString("Culoare");
            }
            if (rs4.next()) {
                mas = rs4.getString("Detalii");
            }
            if (rs4.next()) {
                mas = rs4.getString("Poza");
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return mas;
    }

    // selecteaza 4 anunturi pentru afisare pe prima pagina - random
    public Vector<Masina> getForHomepage() {
        Vector<Masina> results = null ;
        Masina m = null;
        String ProducatorM;
        String ModelM;
        String ProprietarM;
        String An_fabricatie;
        String Combustibil;
        String Culoare;
        String Detalii;
        String Poza;


        try {
            String selectQuery = "select * from masina order by rand() limit 4";

            ResultSet rs = ppauto.getResultSet(selectQuery);
            System.out.println(rs);
            try {
                results = new Vector<Masina>();

                while (rs.next()) {

                    int Serie = rs.getInt("Serie");
                    System.out.println("Serie:" + Serie);
                    ProducatorM = rs.getString("ProducatorM");
                    ModelM = rs.getString("ModelM");
                    ProprietarM = rs.getString("ProprietarM");
                    An_fabricatie = rs.getString("An_fabricatie");
                    Combustibil = rs.getString("Combustibil");
                    Culoare = rs.getString("Culoare");
                    Detalii = rs.getString("Detalii");
                    Poza = rs.getString("Poza");



                    m = new Masina(Serie, ProducatorM, ModelM, ProprietarM, 
                            An_fabricatie, Combustibil, Culoare, Detalii, Poza);


                    results.add(m);
                }
            } catch (SQLException e) {
                // e.printStackTrace();
            }
        } catch (Exception e) {
        }
        return results;

    }

    //realizeaza cautarea
    public Vector<Masina> filter(String p, String m, String a, String c, String S) {


        String[] conditions = new String[4];
        int index = 0;

        if (!p.equals("0")) {
            conditions[index] = " ProducatorM = '" + p + "'" ;
            index++;
        }

        if (!m.equals("0")) {
            conditions[index] = " ModelM =  '" + m + "'" ;
            index++;
        }

        if (!a.equals("0")) {
            conditions[index] = " An_fabricatie = '" + a + "'";
            index++;
        }

        if (!c.equals("0")) {
            conditions[index] = " Combustibil = '" + c + "'" ;

        }

        String sconditions = "";

        int k = conditions.length;
        if (k == 0)
            return null;
        StringBuilder out = new StringBuilder();
        out.append(conditions[0]);
        for (int x = 1; x < index; ++x) {
                out.append(" and ").append(conditions[x]);
        }


        sconditions = out.toString();

    //  System.out.println(conditions);

        Vector<Masina> results = null;
        Masina m1 = null;
        String ProducatorM;
        String ModelM;
        String ProprietarM;
        String An_fabricatie;
        String Combustibil;
        String Culoare;
        String Detalii;
        String Poza;



        try {
            String selectQuery = " select * from masina ";

            if (sconditions != "") {
                selectQuery += " where " + sconditions;
            }

            System.out.println(selectQuery);

            ResultSet rs = ppauto.getResultSet(selectQuery);
            System.out.println(rs);
            try {
                results = new Vector<Masina>();

                while (rs.next()) {

                    int Serie = rs.getInt("Serie");

                    System.out.println("Serie:" + Serie);

                    ProducatorM = rs.getString("ProducatorM");
                    ModelM = rs.getString("ModelM");
                    ProprietarM = rs.getString("ProprietarM");
                    An_fabricatie = rs.getString("An_fabricatie");
                    Combustibil = rs.getString("Combustibil");
                    Culoare = rs.getString("Culoare");
                    Detalii = rs.getString("Detalii");
                    Poza = rs.getString("Poza");


                    m1 = new Masina(Serie, ProducatorM, ModelM, ProprietarM,
                            Combustibil, An_fabricatie, Culoare, Detalii, Poza);

                    results.add(m1);

                }
            } catch (SQLException e) {
                // e.printStackTrace();
            }
        } catch (Exception e) {
        }
        return results;    
    }                
}

1 个解决方案

#1


0  

I can answer why your getting the "EROAR" comes when i<0. By changing the line, you will get a different return value after calling your method. Since we can not see your method we can't help you with that.

我可以回答为什么当我<0时你得到“EROAR”。通过更改该行,您将在调用方法后获得不同的返回值。由于我们无法看到您的方法,我们无法帮助您。

I really don't understand what you want for the rest. Sorry.

我真的不明白你想要什么。抱歉。

#1


0  

I can answer why your getting the "EROAR" comes when i<0. By changing the line, you will get a different return value after calling your method. Since we can not see your method we can't help you with that.

我可以回答为什么当我<0时你得到“EROAR”。通过更改该行,您将在调用方法后获得不同的返回值。由于我们无法看到您的方法,我们无法帮助您。

I really don't understand what you want for the rest. Sorry.

我真的不明白你想要什么。抱歉。