连接 jdbc odbc 驱动程序时出错

Posted

技术标签:

【中文标题】连接 jdbc odbc 驱动程序时出错【英文标题】:Error while connecting jdbc odbc Driver 【发布时间】:2013-12-20 08:44:34 【问题描述】:

我不知道我在哪里做错了,唯一有效的是将记录添加到数据库中。 在 netbeans 上删除和查找代码不起作用(我不包括自动生成的代码)。

错误类型:java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 标准表达式中的数据类型不匹配。

在此处输入代码:

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 * from NameN where ID='value'

代码应该是,

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

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

【讨论】:

以上是关于连接 jdbc odbc 驱动程序时出错的主要内容,如果未能解决你的问题,请参考以下文章

JDBC 驱动程序 MS Access 连接

各类数据库的jdbc驱动及连接字符串总结

如何解决“加载翻译库时出错”Linux ODBC 连接问题?

JDBC ODBC 驱动程序连接

Tableau Server 连接到 ODBC 驱动程序时出错

如何用mysql实现ODBC(JDBC桥)连接数据库?