0
try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection conn= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\DataBase1.mdb","","");
     int id=Integer.parseInt(JOptionPane.showInputDialog("Enter ID"));
     String name=JOptionPane.showInputDialog("Enter Name");
     String Phone=JOptionPane.showInputDialog("Enter PHone");
String mobile=JOptionPane.showInputDialog("Enter Moblie");
//String sql="insert  into friends values "id,'"+home
     Statement ss=conn.createStatement();
       ss.executeUpdate("insert into friends values("+id+",'"+name+"','"+Phone+"','"+mobile+"')");
        Statement s=conn.createStatement();

        ResultSet res=s.executeQuery("select*from friends order by id");
        while(res.next()){
            System.out.println(res.getString(1)+"\t"+res.getString(2)+"\t"+res.getString(3)+"\t"+res.getString(4));

        }
    }catch(Exception e){
     JOptionPane.showMessageDialog(null, e.getMessage());
    }

    }

这是我的代码但是当我运行它时这个异常显示(“java.sql.SQLException: ??[Microsoft][ODBC Driver Manager] ???? ??? ???”)不知道是什么问题请帮忙???

4

1 回答 1

2

使用 PreparedStatement 代替 Statement 怎么样?我认为您的代码不接受硬编码的变量!而且,为了避免陷入更多麻烦,请记住关闭连接。

你还需要星星周围的空间"select * from friends order by id"

于 2012-05-12T19:43:27.773 回答