前题是删除键表里面的信息,我的意思是,我在删除某条记录时候,能否给用户个提示呢,此条记录被使用,不能被删除。
现在只是在我的tomcat里面提示的。
这个是提示信息,
- Java code
com.microsoft.sqlserver.jdbc.SQLServerException: DELETE 语句与 REFERENCE 约束"FK_ZC_Admission_ZC_Nursery"冲突。该冲突发生于数据库"ZC_NmSoft",表"dbo.ZC_Admission", column 'NurseryID'。 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(Unknown Source) at datebase.JDBC.update(JDBC.java:55) at sqlbean.SqlBean.deleteNursery(SqlBean.java:141) at com.yourcompany.struts.action.CollectiveAction.deleteNursery(CollectiveAction.java:123) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270) at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)
这个是我的jdbc
- Java code
package datebase;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JDBC { Connection conn = null; Statement st = null; ResultSet rs = null; PreparedStatement ps; public JDBC() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 2005 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;DatabaseName=ZC_NmSoft", "sa", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { // 创建语句对象 st = conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ResultSet query(String sql) { try { rs = st.executeQuery(sql); } catch (SQLException e) { System.out.println("查询操作异常:" + e); e.printStackTrace(); } return rs; } public int update(String sql) { int i = -1; try { i = st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return i; } public int executeUpdate(String sql)// 更新调用类 { int count = 0; try { st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); count = st.executeUpdate(sql); } catch (Exception ex) { ex.printStackTrace(); } return count; } public PreparedStatement preparedStatement(String sql) { try { ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch (Exception ex) { ex.printStackTrace(); } return ps; } public void close() { try { st.close(); } catch (SQLException e) { System.out.println("关闭stmt异常:" + e); e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { System.out.println("关闭conn异常:" + e); e.printStackTrace(); } }}