You*_*sef 22 java mysql hibernate jpa sqlexception
我怎么能抓住这个例外:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '22-85' for key 'ID_CONTACT'
Run Code Online (Sandbox Code Playgroud)
You*_*sef 25
我用春天所以我们解决它 org.springframework.dao.DataIntegrityViolationException
try {
ao_history_repository.save(new AoHistory(..));
}
catch (DataIntegrityViolationException e) {
System.out.println("history already exist");
}
Run Code Online (Sandbox Code Playgroud)
aun*_*low 10
如果您使用的是Java 1.6+,则捕获SQLIntegrityConstraintViolationException
例如
try {
ps.executeUpdate("INSERT INTO ...");
} catch (SQLIntegrityConstraintViolationException e) {
// Duplicate entry
} catch (SQLException e) {
// Other SQL Exception
}
Run Code Online (Sandbox Code Playgroud)
要么
try {
ps.executeUpdate("INSERT INTO ...");
} catch (SQLException e) {
if (e instanceof SQLIntegrityConstraintViolationException) {
// Duplicate entry
} else {
// Other SQL Exception
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我用的是春天。所以捕获 org.springframework.dao.DuplicateKeyException
try{
...
} catch (DuplicateKeyException dke) {
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59292 次 |
最近记录: |