我在使用jdbc将数据插入到访问中时遇到问题
此声明中出现问题
plz帮助
我的表格有6列 列是id(number),Namee(Short Text),age(number),bsalary(decimal),rate(decimal)和hrs(number)
DBConnect dbconnect = new DBConnect();
dbconnect.getConnection("record.accdb");
Statement statement = dbconnect.getStatement();
String query = "INSERT INTO sguard " +
"VALUES (2, '2', 2, 2.5, 2.6, 2);";
try {
statement.executeUpdate(query);
}
这是错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 integrity constraint violation: unique constraint or index violation; SYS_PK_10386 table: SGUARD
at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:230)
at DB.DBManager.writeAuthor(DBManager.java:97)
at UI.Client.main(Client.java:344)
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10386 table: SGUARD
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
at net.ucanaccess.jdbc.ExecuteUpdate.executeWrapped(ExecuteUpdate.java:65)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:264)
at net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:48)
at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:228)
... 2 more
Caused by: org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; SYS_PK_10386 table: SGUARD
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Constraint.getException(Unknown Source)
at org.hsqldb.index.IndexAVLMemory.insert(Unknown Source)
at org.hsqldb.persist.RowStoreAVL.indexRow(Unknown Source)
at org.hsqldb.TransactionManager2PL.addInsertAction(Unknown Source)
at org.hsqldb.Session.addInsertAction(Unknown Source)
at org.hsqldb.Table.insertSingleRow(Unknown Source)
at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
at org.hsqldb.StatementInsert.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 8 more
这是用于连接和断开连接的DBCOnnect类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnect {
private static Connection connection = null;
private static Statement statement = null;
public static void getConnection(String path) {
try {
String p = "D:\\ACP Practice Projects\\Assignment 3.1.1\\record.accdb";
String url = "jdbc:ucanaccess://"+p;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection(url);
//connection = DriverManager.getConnection("jdbc:ucanaccess://" + path);
statement = connection.createStatement();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("exception");
e.printStackTrace();
}
}
public static Statement getStatement() {
return statement;
}
public static void disconnect() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是DBManager类
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import UI.Employ;
import UI.Lecturer;
import UI.SecurityGuard;
public class DBManager {
public static ArrayList<Employ> viewAuthor(){
ArrayList<Employ> myList = new ArrayList<Employ>();
DBConnect dbconnect = new DBConnect();
dbconnect.getConnection("record.accdb");
Statement statement = dbconnect.getStatement();
String query1 = "SELECT * from lecturer";
String query2 = "SELECT * from sguard";
int col1,col3,col6;
String col2;
float col4,col5;
boolean col7;
try {
ResultSet resultset1=statement.executeQuery(query1);
while(resultset1.next()) {
col1 = resultset1.getInt("id");
col2 = resultset1.getString("Namee");
col3 = resultset1.getInt("age");
col4 = resultset1.getFloat("bsalary");
col5 = resultset1.getFloat("rate");
col6 = resultset1.getInt("courses");
myList.add(new Lecturer(col1,col2,col3,col4,col6,col5));
/*System.out.println(col1);System.out.println(col2);System.out.println(col3);System.out.println(col4);System.out.println(col5);System.out.println(col6);System.out.println(col7);*/
}
ResultSet resultset2=statement.executeQuery(query2);
while(resultset2.next()) {
col1 = resultset2.getInt("id");
col2 = resultset2.getString("Namee");
col3 = resultset2.getInt("age");
col4 = resultset2.getFloat("bsalary");
col5 = resultset2.getFloat("rate");
col6 = resultset2.getInt("hrs");
myList.add(new SecurityGuard(col1,col2,col3,col4,col6,col5));
/*System.out.println(col1);System.out.println(col2);System.out.println(col3);System.out.println(col4);System.out.println(col5);System.out.println(col6);System.out.println(col7);*/
}
//System.out.println(col1 +" ." + col2);
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DBConnect.disconnect();
}
for (int j = 0; j < myList.size(); j++) {
System.out.println(myList.get(j).toString());
}
return myList;
}
public static boolean writeAuthor(ArrayList<Employ> mylist){
DBConnect dbconnect = new DBConnect();
dbconnect.getConnection("record.accdb");
Statement statement = dbconnect.getStatement();
String query = "INSERT INTO sguard " +
"VALUES (2, '2', 2, 2.5, 2.6, 2);";
try {
//ResultSet resultset=statement.executeQuery(query);
statement.executeUpdate(query);
//statement.execute(query, 6);
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DBConnect.disconnect();
}
for (int j = 0; j < mylist.size(); j++) {
System.out.println(mylist.get(j).toString());
}
return false;
}
}