【问题标题】:Error while connecting jdbc odbc Driver连接 jdbc odbc 驱动程序时出错
【发布时间】: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);} }

【问题讨论】:

    标签: java sql swing jdbc


    【解决方案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 +"'");
    

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2020-07-13
      • 2014-11-11
      • 2023-03-31
      相关资源
      最近更新 更多