0

ここに私の問題があります..国IDと国名の2つのフィールドを持つ国テーブルに国名を追加しようとしています。ユーザーが入力したjspページから国名を取得し、テーブルで使用可能なすべての国名をリストしますが、問題は、ユーザーが挿入した名前がリストページに表示されないことです....

ここにコードスニペットがあります

レコード挿入用

public boolean insertCountry(CountryBean bean)
{

String country=bean.getCountryname();

boolean flag=false;

int result;



try
{

    conn=connection.createConnection();


    stmt=conn.createStatement();
    String sqlInsert="insert into country(Country_Name) values('"+country+"')";

    result=stmt.executeUpdate(sqlInsert);






    if(result>0)
        flag=true;
    else
        flag=false;



}




catch(Exception e)
{
    e.printStackTrace();
}

return flag;

}

レコードの表示用

public List ListCountry() は SQLException をスローします {

    List<CountryBean> list=new ArrayList<CountryBean>();
    CountryBean bean;

    Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;



    try
    {

        conn=connection.createConnection();

        stmt=conn.createStatement();

        String sqlselect="select * from country order by country_name";
        rs=stmt.executeQuery(sqlselect);

        if(rs!=null)
        {
            while(rs.next())
            {
                bean=new CountryBean();
                bean.setCountryname(rs.getString("country_name"));
                System.out.println(rs.getString("country_name"));

                list.add(bean);

            }


        }
    }

        catch(Exception e)
        {
            e.printStackTrace();
        }
    finally
    {
        if(rs!=null){
            rs.close();
        }

        if(conn!=null){
            conn.close();
        }
        if(stmt!=null){
            stmt.close();
        }

    }

    return list;

}

}

listing jsp page

<table border="2">
    <tr>
        <th>Country Name</th>

    </tr>
    <%
        for (int i = 0; i < list.size(); i++) {
                CountryBean bean = list.get(i);
    %>
            <tr>
    <td><%=bean.getCountryname()%></td>
             <%
                }
                }
     %>

public static Connection createConnection() { try { Class.forName("com.mysql.jdbc.Driver");

             conn=DriverManager.getConnection("jdbc:mysql://localhost:3309/ecom_db","root","root");

    }
    catch(Exception e)
    {
        e.printStackTrace();


    }
    return conn;
    }                                                                 Name of the country is successfully entered in to the database nothing wrong with the queries but the problem is it does not appear in to the list in table tag.list.size method returns only previously inserted countries not the new one.
4

1 に答える 1

1

あなたの表示する記録機能はひどく台無しに見えます。正しく投稿されていない可能性があります。DB の名前とパスワードを指定して、適切に接続を作成したと仮定します。

Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager
                .getConnection(
                        "jdbc:mysql://localhost:3306/Twitter?jdbcCompliantTruncation=false&amp;useServerPrepStmts=false&amp;rewriteBatchedStatements=true",
                        "root", "xxxx");

あなたの挿入クエリは私には間違っているようです。セミコロンがありません。

  String sqlInsert="insert into country(Country_Name) values('"+country+"');";
  String sqlselect="select * from country order by country_name;";

スローされている例外を投稿します。

編集:OPによって修正されたレコード機能を表示します。という私の誤解。必要とされている。詳細については、これを参照してください。

public static  Connection createConnection()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");

    conn=DriverManager.getConnection("jdbc:mysql://localhost:33/ecom_db","root","root");

        }
        catch(Exception e)
        {
            e.printStackTrace();

        }
        return conn;
        }    
于 2013-03-14T13:33:15.710 に答える