ここに私の問題があります..国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.