2013-12-20 97 views
-1

我不明白我在做錯的地方,唯一要做的就是將記錄添加到數據庫中。 在netbeans上刪除和查找代碼不起作用(我不包括自動生成的代碼)。連接jdbc odbc時出錯驅動程序

錯誤類型:java.sql.SQLException:[Microsoft] [ODBC Microsoft Access 驅動程序]標準表達式中的數據類型不匹配。

輸入代碼在這裏:

import javax.swing.*; 
import java.sql.*; 

public class Connectivity1 extends javax.swing.JFrame { 

    Connection c; 
    Statement s; 
    ResultSet r; 

    public Connectivity1() { 
     initComponents(); 

     try{ 
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        c=DriverManager.getConnection("jdbc:odbc:DataC1"); 
        s=c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        r=s.executeQuery("SELECT * from NameN"); 
         System.out.println("Connected!"); 
         r.next(); 
      } 
     catch(Exception e){ 
      System.out.println(e);} } 


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           

    try{ 
      String id,fname,lname; 
        id=jTextField1.getText(); 
       fname=jTextField2.getText(); 
        lname=jTextField3.getText(); 
        s.executeUpdate("insert into NameN values('"+id+"','"+fname+"','"+lname+"')"); 
       JOptionPane.showMessageDialog(null,"Record has been added!"); 
      DBclose(); 
       DBopen(); 
    } 
     catch(Exception e){ 
       System.out.println(e); 
    } }     



private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           

    try{ 
      String id=JOptionPane.showInputDialog(null,"Enter ID number"); 
       r=s.executeQuery("select * from NameN where ID = " + id +" "); 
      r.next(); 
      SetText(); 
    } 
    catch(Exception e){ 
    System.out.println(e); 
    } }    


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           

    try{ 

      String id=JOptionPane.showInputDialog(null,"Enter ID number"); 
      s.executeUpdate("delete * from NameN where ID = "+id+""); 
        JOptionPane.showMessageDialog(null,"Record has been deleted!"); 
      DBclose(); 
     DBopen(); 
    } 
    catch(Exception e){ 
    System.out.println(e); 
    } } 


public void DBopen(){ 

    try{ 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       c=DriverManager.getConnection("jdbc:odbc:DataC1"); 
       s=c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
       r=s.executeQuery("SELECT * from NameN"); 
       System.out.println("Reconnected!"); 
     } 
    catch(Exception e){ 
     System.out.println(e); 
    } } 

public void DBclose(){ 

    try{ 
      c.close(); 

      System.out.println("Disconnected!"); 
    } 
    catch(Exception e){ 
     System.out.println(e);} } 


public void SetText(){ 

    try{     
      jTextField1.setText(r.getString(1)); 
      jTextField2.setText(r.getString(2)); 
      jTextField3.setText(r.getString(3)); 
      System.out.println("text displayed!"); 
    } 

    catch(Exception e){ 
     System.out.println(e);} } 

回答

1

你應該包括在單引號ID''),因爲它是字符串數據類型

SELECT *來自Namen其中ID = '值'

代碼應該是,

s.executeUpdate("delete * from NameN where ID = '"+id+"'"); 

r=s.executeQuery("select * from NameN where ID = '" + id +"'"); 
+0

由於它的工作。 – user3121949