任何人都可以帮我修复 javasql exception
数据库是locked
. 当我单击jtable
( accounts_table
) 并且该表中的数据转到另一个jtable
( accounts_all
) 时,当我尝试通过单击jbutton
( acct_create
) 创建新帐户时,数据库将成功打开,但之后JOptionPane
将显示并且其中的消息是 java sql 异常数据库是locked
。
private void acct_createActionPerformed(java.awt.event.ActionEvent evt) {
int prompt = JOptionPane.showConfirmDialog(null, "Are those data correct?", "Message", JOptionPane.YES_NO_OPTION);
if(prompt == 0){
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Edz\\Documents\\NetBeansProjects\\bantug\\bantug_coop.sqlite");
JOptionPane.showMessageDialog(null, "Opened database successfully");
stmt = conn.createStatement();
String value2 = account_name.getText();
String sql = "CREATE TABLE '"+(value2)+"' " +
"(type CHAR, " + " account_number VARCHAR, " + " account_name VARCHAR, " + " home_address VARCHAR, " + " telno_home VARCHAR, " + " business_address VARCHAR, " + " telno_business VARCHAR, " + " initial_deposit DOUBLE, " + " withdrawals DOUBLE, " +" balance DOUBLE, " +" posted_by VARCHAR, " + " approved_by VARCHAR, " + " date DATETIME, " + " days INT, " + " interest DOUBLE, " + " interest_annum DOUBLE, " + " member_type CHAR, " + " fees DOUBLE)";
stmt.executeUpdate(sql);
sql = "insert into '"+(value2)+"' (type, account_number, account_name, home_address, telno_home, business_address, telno_business, initial_deposit, posted_by, approved_by, date, days, interest, balance, member_type, fees) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pst = conn.prepareStatement(sql);
DecimalFormat df = new DecimalFormat("#.00");
pst.setString(1, account_type.getText());
pst.setString(2, account_number.getText());
pst.setString(3, account_name.getText());
pst.setString(4, account_home.getText());
pst.setString(5, account_hometelno.getText());
pst.setString(6, account_business.getText());
pst.setString(7, account_bustelno.getText());
pst.setString(8, account_deposit.getText());
pst.setString(9, account_posted.getText());
pst.setString(10, account_approved.getText());
Date dateFromDateChooser = account_date.getDate();
String dateString = String.format("%1$tY-%1$tm-%1$td", dateFromDateChooser);
pst.setString(11, dateString);
pst.setInt(12, 0);
double rateday = 0.03/360;
double total = Double.parseDouble(account_deposit.getText());
String deposit = df.format(new Double(total)).toString();
double enter_value = new Integer(0);
double total2 = total* rateday * enter_value;
String total3= df.format(new Double(total2)).toString();
pst.setString(13, total3);
pst.setString(14, deposit);
String value = member_type.getSelectedItem().toString();
pst.setString(15, value);
double fee = 0;
String fee_int = new Double(fee).toString();
pst.setString(16, fee_int);
pst.execute();
JOptionPane.showMessageDialog(null, "Account Data saved!");
try{
sql = "select balance, days from '"+(value2)+"' where rowid = last_insert_rowid()";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
try{
double withdraw = 0;
double sum = rs.getDouble("balance");
double days = rs.getDouble("days");
double amount = sum * rateday * days;
double sum_amount = sum+amount;
String new_amount = df.format(new Double(sum_amount)).toString();
sql = "update '"+(value2)+"' set interest_annum='"+new_amount+"', withdrawals='"+withdraw+"' where rowid = last_insert_rowid()";
pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Interest data saved!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}
//for pbook printing data
sql = "select * from '"+value2+"' where rowid = last_insert_rowid()";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
String with = rs.getString("withdrawals");
pbook_with.setText(with);
String dep = rs.getString("initial_deposit");
pbook_dep.setText(dep);
if(with == "0" && dep == "0"){
type_trnscn.setText("wthdrw");
}else if(with != "0" && dep != "0"){
type_trnscn.setText("dep");
}
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
if(((month+1)+"-"+day).matches("3-31")||((month+1)+"-"+day).matches("6-30")||((month+1)+"-"+day).matches("9-30")||((month+1)+"-"+day).matches("12-31")){
String inter = rs.getString("interest");
pbook_interest.setText(inter);
}else if(((month+1)+"-"+day)!="3-31"||((month+1)+"-"+day)!="6-30"||((month+1)+"-"+day)!="9-30"||((month+1)+"-"+day)!="12-31"){
pbook_interest.setText("0");
}
sql = "select sum(initial_deposit), sum(withdrawals), interest_annum from '"+value2+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
if(((month+1)+"-"+day).matches("3-31")||((month+1)+"-"+day).matches("6-30")||((month+1)+"-"+day).matches("9-30")||((month+1)+"-"+day).matches("12-31")){
String adb = rs.getString("interest_annum");
pbook_bal.setText(adb);
}else if(((month+1)+"-"+day)!="3-31"||((month+1)+"-"+day)!="6-30"||((month+1)+"-"+day)!="9-30"||((month+1)+"-"+day)!="12-31"){
String sumdep = rs.getString("sum(initial_deposit)");
String sumwith = rs.getString("sum(withdrawals)");
double depval = Double.parseDouble(sumdep);
double withval = Double.parseDouble(sumwith);
double value_amount = depval-withval;
String total_amount = Double.toString(value_amount);
pbook_bal.setText(total_amount);
}
}
}
sql = "select rowid as 'Account ID', type as 'Type', account_number as 'Account No.', account_name as 'Account Name', home_address as 'Home Address', telno_home as 'Tel. No. Home', business_address as 'Business Address', telno_business as 'Business Tel. No.', initial_deposit as 'Deposit', withdrawals as 'Withdrawals', balance as 'Balance', posted_by as 'Posted By', approved_by as 'Approved By', date as 'Date', days as 'No. of Days', interest as 'Interest per ADB', interest_annum as 'Total Balance', member_type as 'Membership Type', fees as 'Charges' from '"+value2+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
accounts_all.setModel(DbUtils.resultSetToTableModel(rs));
accounts_all.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
try{//add data to Savings Table
sql = "select * from '"+value2+"' where rowid = last_insert_rowid()";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
String type = rs.getString("type");
String no = rs.getString("account_number");
String name = rs.getString("account_name");
String h_add = rs.getString("home_address");
String h_tel = rs.getString("telno_home");
String b_add = rs.getString("business_address");
String b_tel = rs.getString("telno_business");
String i_dep = rs.getString("initial_deposit");
String withdraw = rs.getString("withdrawals");
String bal = rs.getString("balance");
String post = rs.getString("posted_by");
String approved = rs.getString("approved_by");
String date = rs.getString("date");
String days = rs.getString("days");
String interest = rs.getString("interest");
String int_annum = rs.getString("interest_annum");
String member_type = rs.getString("member_type");
String c_fee = rs.getString("fees");
String acct_code = new Integer(21100).toString();
sql = "insert into Savings (acct_code,type,date,member_type,account_number,account_name,home_address,telno_home,business_address,telno_business,initial_deposit,posted_by,approved_by,days,interest,balance,interest_annum,withdrawals,fees) values ('"+acct_code+"', '"+type+"', '"+date+"', '"+member_type+"', '"+no+"', '"+name+"', '"+h_add+"', '"+h_tel+"', '"+b_add+"', '"+b_tel+"', '"+i_dep+"', '"+post+"', '"+approved+"', '"+days+"', '"+interest+"', '"+bal+"', '"+int_annum+"', '"+withdraw+"', '"+c_fee+"')";
pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Savings Data Saved");
sql = "insert into Savings_Master (acct_code,type,date,member_type,account_number,account_name,home_address,telno_home,business_address,telno_business,initial_deposit,posted_by,approved_by,days,interest,balance,interest_annum,withdrawals,fees) values ('"+acct_code+"', '"+type+"', '"+date+"', '"+member_type+"', '"+no+"', '"+name+"', '"+h_add+"', '"+h_tel+"', '"+b_add+"', '"+b_tel+"', '"+i_dep+"', '"+post+"', '"+approved+"', '"+days+"', '"+interest+"', '"+bal+"', '"+int_annum+"', '"+withdraw+"', '"+c_fee+"')";
pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Savings Master Data saved");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
System.exit(0);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
UpdateTable();
stmt.close();
conn.close();
} catch ( Exception e ) {
JOptionPane.showMessageDialog(null, e);
System.exit(0);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
JOptionPane.showMessageDialog(null, "Table created successfully");
}
}
这是jtable
代码:
private void accounts_tableMouseClicked(java.awt.event.MouseEvent evt) {
try{//print p_book data
int row = accounts_table.getSelectedRow();
String table_click = (accounts_table.getModel().getValueAt(row, 0).toString());
String sql = "select * from '"+table_click+"' order by rowid DESC limit 1";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
String with = rs.getString("withdrawals");
pbook_with.setText(with);
String dep = rs.getString("initial_deposit");
pbook_dep.setText(dep);
if(with == "0" && dep == "0"){
type_trnscn.setText("wthdrw");
}else if(with != "0" && dep != "0"){
type_trnscn.setText("dep");
}
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
if(((month+1)+"-"+day).matches("3-31")||((month+1)+"-"+day).matches("6-30")||((month+1)+"-"+day).matches("9-30")||((month+1)+"-"+day).matches("12-31")){
String inter = rs.getString("interest");
pbook_interest.setText(inter);
}else if(((month+1)+"-"+day)!="3-31"||((month+1)+"-"+day)!="6-30"||((month+1)+"-"+day)!="9-30"||((month+1)+"-"+day)!="12-31"){
pbook_interest.setText("0");
}
sql = "select sum(initial_deposit), sum(withdrawals), interest_annum from '"+table_click+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()){
if(((month+1)+"-"+day).matches("3-31")||((month+1)+"-"+day).matches("6-30")||((month+1)+"-"+day).matches("9-30")||((month+1)+"-"+day).matches("12-31")){
String adb = rs.getString("interest_annum");
pbook_bal.setText(adb);
}else if(((month+1)+"-"+day)!="3-31"||((month+1)+"-"+day)!="6-30"||((month+1)+"-"+day)!="9-30"||((month+1)+"-"+day)!="12-31"){
String sumdep = rs.getString("sum(initial_deposit)");
String sumwith = rs.getString("sum(withdrawals)");
double depval = Double.parseDouble(sumdep);
double withval = Double.parseDouble(sumwith);
double value = depval-withval;
String total = Double.toString(value);
pbook_bal.setText(total);
}
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
try{
int row = accounts_table.getSelectedRow();
String table_click = (accounts_table.getModel().getValueAt(row, 0).toString());
String sql = "select rowid as 'Account ID', type as 'Type', account_number as 'Account No.', account_name as 'Account Name', home_address as 'Home Address', telno_home as 'Tel. No. Home', business_address as 'Business Address', telno_business as 'Business Tel. No.', initial_deposit as 'Deposit', withdrawals as 'Withdrawals', balance as 'Balance',posted_by as 'Posted By', approved_by as 'Approved By', date as 'Date', days as 'No. of Days', interest as 'Interest per ADB', interest_annum as 'ADB', member_type as 'Type of Member', fees as 'Charges' from '"+table_click+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
accounts_all.setModel(DbUtils.resultSetToTableModel(rs));
accounts_all.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
int index = 0;
while (index < 19){
if(index == 0){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(70);
}else if(index == 1){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(70);
}else if(index == 2){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(150);
}else if(index == 3){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(200);
}else if(index == 4){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(300);
}else if(index == 5){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(150);
}else if(index == 6){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(300);
}else if(index == 7){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(150);
}else if(index == 8){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(250);
}else if(index == 9){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(200);
}else if(index == 10){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(200);
}else if(index == 11){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(150);
}else if(index == 12){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(90);
}else if(index == 13){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(100);
}else if(index == 14){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(100);
}else if(index == 15){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(100);
}else if(index == 16){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(100);
}else if(index == 17){
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(100);
}else{
TableColumn ut =accounts_all.getColumnModel().getColumn(index);
ut.setPreferredWidth(150);
}
index+=1;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
UpdateTable();
}
这是我的 sql 连接代码:
public class sqlconnect {
Connection conn = null;
public static Connection ConnectorDb(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Edz\\Documents\\NetBeansProjects\\bantug\\bantug_coop.sqlite");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}