2012-05-12 4 views
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ドライバマネージャ]を???? ??? ???") idontどのような問題が喜んでヘルプ知っている???私はJavaアプリケーションをWindows 7 x64のデータベースに接続する必要がありますか?私はそれ この例外ショー実行時に

+0

ラインで例外が発生ん周りのスペースが必要ですか?接続マネージャー= DriverManager.getConnection( "jdbc:odbc:ドライバー= {Microsoft Accessドライバー(* .mdb、* .accdb)}; DBQ = D:\\ DataBase1.mdb"、 ""、 ""この行の – Ben

+0

: ); –

答えて

2

ステートメントの代わりにPreparedStatementを使用することはどうですか?あなたのコードは、ハードコードされた変数を受け入れていないと思います!さらにトラブルを避けるために、接続を閉じることを覚えておいてください。

はまた、あなたはスター"select * from friends order by id"

+0

偉大なベンを見つける!いいぞ! –

関連する問題