so here is my problem.. i am trying to add country name in country table having a two fields country id and country name. i am taking a country name through jsp page entered by user and list all the country names available in a table but the problem is whatever the name inserted by user does not display in listing page....
here is code snippets
for inserting record
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;
}
for displaying records
public List ListCountry() throws 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.