Java 最后插入的记录不会反映在使用JDBC的mysql中的select语句中

Java 最后插入的记录不会反映在使用JDBC的mysql中的select语句中,java,mysql,jsp,jakarta-ee,jdbc,Java,Mysql,Jsp,Jakarta Ee,Jdbc,这就是我的问题。。 我试图在country表中添加country name,该表有两个字段country id和country name。我通过用户输入的jsp页面获取一个国家名称,并在一个表中列出所有可用的国家名称,但问题是,无论用户插入的名称在列表页面中显示什么 下面是代码片段 用于插入记录 public boolean insertCountry(CountryBean bean) { String country=bean.getCountryname(); boolean flag

这就是我的问题。。 我试图在country表中添加country name,该表有两个字段country id和country name。我通过用户输入的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;
}

用于显示记录

公共列表ListCountry()引发SQLException {


您的显示记录功能看起来非常混乱。可能未正确发布。 我假设您已经通过为DB提供名称和密码正确地创建了连接

Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager
                .getConnection(
                        "jdbc:mysql://localhost:3306/Twitter?jdbcCompliantTruncation=false&useServerPrepStmts=false&rewriteBatchedStatements=true",
                        "root", "xxxx");
我认为您的insert查询出错。缺少分号

  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;
        }    

公共静态连接createConnection(){try{Class.forName(“com.mysql.jdbc.Driver”);conn=DriverManager.getConnection(“jdbc:mysql://localhost:3309/ecom_db“,”根“,”根“;}捕获(异常e){e.printStackTrace();}返回连接;}国家名称已成功输入到数据库中查询没有问题,但问题是它没有出现在表tag.list.size方法的列表中。方法仅返回以前插入的国家,而不是新的国家。是否在数据库中运行相同的检索查询e是否适合您?是的,如果我在数据库中编写相同的查询,它在新添加的国家/地区执行良好,但在编码中不起作用。不知道为什么?我没有看到事务提交,您的连接是否设置为自动提交?不要将HTML与Java代码混淆。请避免这种令人讨厌的行为,因为它非常不受欢迎,并且总是用于缓解SQLEjection attacks.OP在我的答案的注释部分发布了连接语句。由于连接字符串中没有参数,因此将其设置为自动提交模式。
Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager
                .getConnection(
                        "jdbc:mysql://localhost:3306/Twitter?jdbcCompliantTruncation=false&useServerPrepStmts=false&rewriteBatchedStatements=true",
                        "root", "xxxx");
  String sqlInsert="insert into country(Country_Name) values('"+country+"');";
  String sqlselect="select * from country order by country_name;";
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;
        }