Java 连接jdbc odbc驱动程序时出错
我不知道我做错了什么,唯一有效的方法就是将记录添加到数据库中。 删除和查找代码在netbeans上不起作用(我不包括自动生成的代码) 错误类型:java.sql.SQLException:[Microsoft][ODBC Microsoft Access 驱动程序]条件表达式中的数据类型不匹配 在此处输入代码:Java 连接jdbc odbc驱动程序时出错,java,sql,swing,jdbc,Java,Sql,Swing,Jdbc,我不知道我做错了什么,唯一有效的方法就是将记录添加到数据库中。 删除和查找代码在netbeans上不起作用(我不包括自动生成的代码) 错误类型:java.sql.SQLException:[Microsoft][ODBC Microsoft Access 驱动程序]条件表达式中的数据类型不匹配 在此处输入代码: import javax.swing.*; import java.sql.*; public class Connectivity1 extends javax.swing.JFra
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);} }
您应该在单引号(
'
)中包含ID
,因为它是字符串数据类型
选择*from NameN,其中ID='value'
代码应该是
s.executeUpdate("delete * from NameN where ID = '"+id+"'");
及
r=s.executeQuery("select * from NameN where ID = '" + id +"'");