连接jdbc odbc驱动程序时出错

时间:2013-12-20 08:44:34

标签: java sql swing jdbc

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

  

错误类型: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 个答案:

答案 0 :(得分:1)

您应该将ID包含在单引号('')中,因为它是字符串数据类型

  

从NameN中选择*,其中ID ='value'

代码应该是,

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

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