不好意思我遇到一個很棘手的問題,我在用Java連結Sqlite的時候一直無法順利修改到資料庫內:
首先我先創建一個資料表
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import org.sqlite.JDBC;
public class TestSQLite
{
public static String getTable()
{
return "CREATE TABLE MYSTUDENTDATA " +
"(number INT PRIMARY KEY NOT NULL," +
" lastname TEXT NOT NULL, " +
" firstname TEXT NOT NULL, " +
" nickname TEXT NOT NULL)";
}
public static void main(String args[])
{
Connection c = null;
Statement stmt = null;
try
{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:data/MYSTUDENTDATA.db");
stmt = c.createStatement();
stmt.executeUpdate(getTable());
}
catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
finally
{
try
{
if(null != stmt)
{
stmt.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if(null != c)
{
c.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
System.out.println("Create Table successfully");
System.exit(0);
}
}
並且它也產生了對應的DB(如下圖)
然後這我撰寫與修改的程式碼-名為Modify.java
//By LLW
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Modify extends JFrame
{
private static final long serialVersionUID = -1928970409928880648L;
JLabel jlnumber = new JLabel("Student ID: ");
JLabel jllname = new JLabel("Last Name: ");
JLabel jlfname = new JLabel("First Name: ");
JLabel jlnname = new JLabel("Nick Nmae: ");
JTextField jtnumber = new JTextField("",20);
JTextField jtlname = new JTextField("",20);
JTextField jtfname = new JTextField("",20);
JTextField jtnname = new JTextField("",20);
JButton buttonchange = new JButton("Modify");
JButton buttonreturn = new JButton("Back");
public Modify()
{
JPanel jpnumber = new JPanel();
JPanel jplname = new JPanel();
JPanel jpfname = new JPanel();
JPanel jpnname = new JPanel();
JPanel jpforbutton = new JPanel(new GridLayout(1,1));
jpnumber.add(jlnumber);
jpnumber.add(jtnumber);
jplname.add(jllname);
jplname.add(jtlname);
jpfname.add(jlfname);
jpfname.add(jtfname);
jpnname.add(jlnname);
jpnname.add(jtnname);
jpforbutton.add(buttonchange);
jpforbutton.add(buttonreturn);
buttonchange.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String number = jtnumber.getText();
String lastname = jtlname.getText();
String firstname = jtfname.getText();
String nickname = jtnname.getText();
Connection conn = null;
ResultSet res = null;
Statement stat = null;
String sql = "SELECT * FROM MYSTUDENTDATA";
try
{
Class.forName("org.sqlite.JDBC");
}
catch(Exception d)
{
System.out.println("JBDC Failure!");
d.printStackTrace();
}
try
{
conn=DriverManager.getConnection("jdbc:sqlite:Data/MYSTUDENTDATA.db");
stat=conn.createStatement();
res=stat.executeQuery(sql);
while (res.next())
{
//modify
if (res.getString(1).equals(jtnumber.getText()))
{
try
{
Class.forName("org.sqlite.JDBC");
}
catch(Exception d)
{
System.out.println("JBDC Failure!");
d.printStackTrace();
}
String sql1="UPDATE MYSTUDENTDATA SET lastname='"+lastname+"' WHERE number='"+jtnumber.getText()+"'";
String sql2="UPDATE MYSTUDENTDATA SET firstname='"+firstname+"' WHERE number='"+jtnumber.getText()+"'";
String sql3="UPDATE MYSTUDENTDATA SET nickname='"+nickname+"' WHERE number='"+jtnumber.getText()+"'";
try
{
conn=DriverManager.getConnection("jdbc:sqlite:Data/MYSTUDENTDATA.db");
stat=conn.createStatement();
stat.executeUpdate(sql1);
stat.executeUpdate(sql2);
stat.executeUpdate(sql3);
}
catch (SQLException g)
{
// TODO Auto-generated catch block
g.printStackTrace();
}
try
{
stat.close();
conn.close();
}catch(SQLException ar)
{
ar.printStackTrace();
}
break;
}
//change end
}
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally
{
try
{
conn.close();
}
catch(SQLException ar)
{
ar.printStackTrace();
}
}
}
}
);
buttonreturn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Window window = new Window();
}
}
);
this.setTitle("Modify Student Information");
this.setLayout(new GridLayout(9,1));
this.add(jpnumber);
this.add(jplname);
this.add(jpfname);
this.add(jpnname);
this.add(jpforbutton);
this.setLocation(400,300);
this.setSize(350,300);
this.setVisible(true);
}
}
系統提示應該已成功載入,但就是寫不進去OTZ
最後這是錯誤警告
Successfully Loaded!
Successfully Closed!
MySQL Closed Successfully!
JBDC Successfully Loaded!
MySQL Closed Successfully!
java.sql.SQLException: database is locked
at org.sqlite.NativeDB.throwex(NativeDB.java:210)
at org.sqlite.NativeDB._exec(Native Method)
at org.sqlite.Stmt.executeUpdate(Stmt.java:152)
at Modify$1.actionPerformed(Modify.java:99)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Successfully Loaded!
Successfully Closed!
不知道您們能否給個提示呢?希望您們可以協助我解決,真的非常感謝!!!
更新: 很感謝"渊渟无迹静涛君"與"sanshizhang"大大們的幫助!我在稍早前確實也發現了這問題,所以我也進行了修改(如上修改),但它卻仍然表示錯誤...說被鎖定....是我哪裡寫錯了嗎?
更新: 沒事了,我發現是我寫的"修改功能的程式碼有問題"但已修復,"不是沒有關閉的鍋"(因為我寫的新增、刪除與檢視並無問題且都有"關閉"的設定)...真的非常感謝您們的幫忙OTZ