java - 未检测到 JDBC MySQL 新插入

标签 java mysql jdbc server

我正在使用 Mysql 数据库和 jdbc 连接的 java 服务器工作

当我尝试执行以下步骤时出现问题

1.启动服务器

2.在数据库中手动插入(使用 phmypadmin)一条新记录。

3.使用jdbc api选择记录

我只获取数据库中已有的记录,而不是新记录。

这是我的连接:

 public JDBCMySQL(String nomFitxerPropietats) throws Exception {
        if (nomFitxerPropietats == null) {
            nomFitxerPropietats = "jdbcMysql.txt";
        }
        Properties props = new Properties();
        try {
            props.load(new FileReader(nomFitxerPropietats));
        } catch (FileNotFoundException ex) {
            throw new Exception("No es troba fitxer de propietats", ex);
        } catch (IOException ex) {
            throw new Exception("Error en carregar fitxer de propietats", ex);
        }
        String driver = props.getProperty("driver");
        if (driver == null && System.getProperty("java.version").compareTo("1.6") < 0) {
            throw new Exception("La versió de Java obliga a definir la propietat driver dins el fitxer de propietats");
        }
        String url = props.getProperty("url");
        if (url == null) {
            throw new Exception("Manca la propietat url en el fitxer de propietats");
        }
        String user = props.getProperty("user");
        String password = props.getProperty("password");
        // No controlem !=null per què hi ha SGBDR que permeten no indicar user/contrasenya (MsAccess)
        try {
            if (driver != null) {
                Class.forName(driver);
            }
            con = DriverManager.getConnection(url, user, password);
            con.setAutoCommit(false);
        } catch (SQLException ex) {
            throw new Exception("No es pot establir connexió", ex);
        } catch (ClassNotFoundException ex) {
            throw new Exception("No es pot carregar la classe " + driver);
        }

    }

这是我用来检索记录的函数:

  @Override
    public List<Cita> getCitas(int sesID) {
    List<Cita> citas = new ArrayList<Cita>();
     try {

     Statement st = null;
    ResultSet rs = null;

       String consulta = "  select iii.estatinforme,s.NUMERO,c.diahora, s.PERIT_NO,s.INFORME_NO,s.POLISSA_ID,s.DATAASSIGNACIO,s.DATAOBERTURA,s.DATATANCAMENT,s.DESCRIPCIOSINISTRE,s.TIPUSSINISTRE,s.ESTATSINISTRE,s.polissa_id,p.municipi from SINISTRE s join POLISSA p on (s.polissa_id = p.ID_POLISSA) join PERIT pe on ( pe.NUMERO = s.PERIT_NO) join CITA c on (c.sinistre_id = s.numero) left join INFORMEPERICIAL iii on (c.sinistre_id=iii.sinistre_id) where ( s.PERIT_NO = ? and (iii.ESTATINFORME = 'PENDENT'  or iii.ESTATINFORME is null  or DATE_FORMAT(c.diahora, '%Y-%m-%d') = CURDATE()   )) order by c.diahora ";

       PreparedStatement pstmt = con.prepareStatement(consulta);

       pstmt.setInt(1, sesID);

      rs =  pstmt.executeQuery();

        while (rs.next()) {
          //handling my reads...

        }
         pstmt.close();
           rs.close();
  } catch (SQLException ex) {
      Logger.getLogger(JDBCMySQL.class.getName()).log(Level.SEVERE, null, ex);
  }

    return citas;
}

我怀疑它与准备好的语句或查询无关,因为在我停止服务器并再次启动服务器后,会使用函数 GetCitas() 检索新的语句。

编辑:我正在使用的配置文件(jdbcMysql.txt):

url=jdbc:mysql://MyIP:3306/mydb

user=myuser

password=mypass

最佳答案

总而言之,这似乎是 Transaction Isolation 的问题。 。

基本上,只要某个事务尚未提交插入或更新的数据,您就无法在另一个程序中看到它。

同样,只要您的事务处于打开状态,您将始终收到与事务开始时数据库相同的状态。

设置 autocommit=false 后,您在程序中很早就开始了该事务,只要您不 commit() 您的事务,您就会看到相同的快照(至少在默认情况下) mysql事务隔离REPEATABLE READ到位)

这就是为什么在选择起作用之前手动 commit() 连接您的原因。

consistent read

A read operation that uses snapshot information to present query results based on a point in time, regardless of changes performed by other transactions running at the same time. If queried data has been changed by another transaction, the original data is reconstructed based on the contents of the undo log. This technique avoids some of the locking issues that can reduce concurrency by forcing transactions to wait for other transactions to finish.

关于另一个主题:您的代码可以从一些清理中受益匪浅 - 请参阅以下使用 try-with-resource 的 getCitas 实现:

    @Override
    public List<Cita> getCitas(int sesID) {
      List<Cita> citas = new ArrayList<Cita>();
      String consulta = "select iii.estatinforme,s.NUMERO, [...]";
      try (PreparedStatement pstmt = con.prepareStatement()) {
          pstmt.setInt(1, sesID);
          try(ResultSet rs =  pstmt.executeQuery()) {
              while (rs.next()) {
                  //handling my reads...
              }
          }
      } //Closing happend automatically! Even in case of an error!
      catch (SQLException ex) {
          Logger.getLogger(JDBCMySQL.class.getName()).log(Level.SEVERE, null, ex);
      }     
      return citas;
    }

关于java - 未检测到 JDBC MySQL 新插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285659/

相关文章:

Java解析args中的JSON

c# - 奇怪的字符串操作方法

mysql - 选择表 MySQL 中的最低值

php - 无法使用PHP访问远程MYSQL数据库

java - 运行时找不到 JDBC 类

java - 64 位 Windows 中与 Access 数据库的 ODBC 连接

java - 使用 JPA/hibernate 时是否可以将 orm.xml 定义与注释混合在一起?

mysql - 在没有 EAV 的情况下存储属性/选项数据

java - 我收到以下代码的 SQL 语法错误和许多异常

java - 查找和替换对话框