【发布时间】:2017-05-31 13:05:46
【问题描述】:
我正在使用 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
用户=我的用户
密码=我的密码
【问题讨论】:
-
您确定要在 phpmyadmin 会话中提交您的事务吗?此外,虽然 JDBC 不缓存任何内容,但可能您的服务器设置了某种数据库缓存,请检查您的配置。
-
是的,很确定。
-
最好只在需要时打开连接,并在必要的最小范围内打开。我有一种感觉,您可能会在程序期间打开连接。 (我认为问题在于,在更新数据库之前打开了连接,因此它与数据库的当前状态没有连接,因此当您进行检索时,不会检索添加的行。)最好将连接作为一个字段删除,并使其成为每个方法的局部变量。这样您就可以控制何时打开和关闭连接。
-
根据documentation,“在连接的上下文中执行SQL语句并返回结果。”
-
@Ishnark:不。恰恰相反。在大型企业应用程序中,您始终保持连接池打开,因为打开与数据库的连接非常昂贵。相反,您将尝试限制完全不相关的 事务大小。