2
我有下面的代碼,它給了我這個錯誤。我需要在數據庫中顯示通過TCP連接傳輸的值。幫幫我? 當我註釋Class.forName(「org.sqlite.JDBC」)和conexiune.setAutoCommit(false)之間的界限時;還有那些在IF中從String到conexiune.commit()的工作很好,似乎客戶端和服務器之間的連接是可以的。SQLITE_ERROR SQL錯誤或缺少數據庫(接近「VALUES」:語法錯誤)
這是類服務器的方式運行:
public void run() {
while(true)
{
try
{
soc=server.accept();
System.out.println("OK!");
ObjectInputStream ois=new ObjectInputStream(soc.getInputStream());
Class.forName("org.sqlite.JDBC");
Connection conexiune=DriverManager.getConnection("jdbc:sqlite:BazaDeDate.db");
System.out.println("Conexiunea la baza de date s-a realizat cu succes!");
Statement comanda=(Statement) conexiune.createStatement();
conexiune.setAutoCommit(false);
Object obj=ois.readObject();
if(obj instanceof AvionPasageri)
{
AvionPasageri ap=(AvionPasageri)obj;
System.out.println(ap.toString());
String s="";
for(String sir : ap.getCnpPasageri())
{
s+=sir;
}
((java.sql.Statement) comanda).executeUpdate("INSERT INTO AvionPasageri(serie,tonaj,marca,capacitate,cnp VALUES(' "
+ap.getSerie()+"', '"+ap.getTonaj()+"', '"+ap.getMarca()+"', '"+ap.getCapacitate()
+"', '"+s+"')");
System.out.println("Inserat!");
conexiune.commit();
}
else if(obj instanceof AvionCargo)
{
AvionCargo ac=(AvionCargo)obj;
System.out.println(ac.toString());
String s="";
for(String sir: ac.getSerieMarfuri())
{
s+=sir;
}
((java.sql.Statement) comanda).executeUpdate("INSERT INTO AvionPasageri(serie,tonaj,marca,capacitate,serii VALUES('"
+ac.getSerie()+"', '"+ac.getTonaj()+"', '"+ac.getMarca()+"', '"+ac.getCapacitate()
+"', '"+s+"')");
System.out.println("Inserat!");
conexiune.commit();
}
conexiune.close();
soc.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
我也有數據庫這裏的類:
public class CreareBazaDeDate
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Class.forName("org.sqlite.JDBC");
Connection conexiune=DriverManager.getConnection("jdbc:sqlite:BazaDeDate.db");
Statement comanda=(Statement) conexiune.createStatement();
conexiune.setAutoCommit(false);
((java.sql.Statement) comanda).executeUpdate("CREATE TABLE AvionPasageri(serie string, tonaj float, marca string, capacitate float, cnp string)");
((java.sql.Statement) comanda).executeUpdate("CREATE TABLE AvionCargo(serie string, tonaj float, marca string, capacitate float, marfa string)");
conexiune.commit();
conexiune.close();
}
}
如果它可以幫助你,這是他的類服務器的第一部分,在運行之前():
public class Server implements Runnable
{
public ServerSocket server;
public Socket soc;
public static void main(String[] args)
{
Thread t=new Thread(new Server());
t.start();
}
public Server()
{
try
{
server=new ServerSocket(5200);
}
catch(IOException e)
{
e.printStackTrace();
}
}
你能幫我弄清楚有什麼問題嗎?
我又增加了關閉paranthesis和我仍然得到這個錯誤... – Jason
哦,我加錯了。我從來沒有想過這會來自一個小小的假設。謝謝! @mikep – Jason
不客氣:) – mikep