try..catch块就行了,举个栗子:public static void main(String[] args) {
           try {
        DriverManager.getConnection("xxxx");
} catch (MySQLIntegrityConstraintViolationException e) {
// TODO: handle exception
} catch (SQLException e){
// TODO: handle exception
}
   }