2016-05-21 8 views
0

私はデータベースに接続しようとしています。それは正常に動作していました。しかし、今ではいくつかの瓶が削除されているか、私が知らないもの(赤い十字架が来ている)があります。しかし、私はClass.forName( "com.mysql.jdbc.Driver")でclassnotfound例外を取得していますが、もう一度すべてのjarファイルをダウンロードしました(mysql-connector、commons-io-2.4)。ここ これは私もそれは私のために働いていないトライキャッチを使用してみましたが、エラーjava.lang.ClassNotFoundExceptionを取得する:javaのcom.mysql.jdbc.Driver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Unknown Source) 
at deiniteguide.JDBC_example.main(JDBC_example.java:24) 

である私のコード

package abc; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.sql.Statement; 

    public class JDBC_oracle { 
    public static void main(String[] args) throws ClassNotFoundException, SQLException { 
    //step1 load the driver class 
    Class.forName("com.mysql.jdbc.Driver"); 
    //step2 create the connection object 
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd"); 
    //step3 create the statement object 
    Statement statement = connection.createStatement(); 
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query");  
    while(rs.next()) 
     System.out.println("I am triyng to print data"); 
//step5 close the connection object 
    connection.close(); 
} 
    } 

です。

+1

mysql-connector.jarファイルはクラスパスにありません。無関係ですが、 'Class.forName()'はもう必要ありません。 JDBCドライバは、クラスパス上にある限り、自動的に登録されます。 – Andreas

答えて

1

netbeansを使用している場合は、プロジェクトのライブラリフォルダに移動できます。右クリックしてライブラリを追加し、mysql driverを選択します。申し訳ありませんが、評判が低いためにコメントできませんでした。あなたが受け取ったエラーメッセージを投稿できると簡単になります。また、エラーを見つけるのが簡単になるように、プログラムでtry catchを使用してください。あなたのコードは次のようになります:

public class JDBC_oracle { 
    public static void main(String[] args) throws ClassNotFoundException, SQLException { 
    //step1 load the driver class 
    try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
     }catch(Exception e){ 
     System.out.println("Class not found" +e); 
    //step2 create the connection object 
    try{ 
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd"); 
    }catch(Exception es){ 
    System.out.println("connection couldn't be made "+es); 
     } 
    //step3 create the statement object 
    try{ 
    Statement statement = connection.createStatement(); 
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query"); } 
    catch(Exception er){ 
     System.out.println("Query wasnot executed "+er);  
    } 
    while(rs.next()) 
     System.out.println("I am triyng to print data"); 
//step5 close the connection object 
    connection.close(); 
} 
    } 
+0

私は私の質問に間違いを追加しました。 – monil

+0

あなたはnetbeansを使用していますか?または日食?それはあなたのideプロジェクト用のMySqlドライバを設定するのにも役立ちます –

関連する問題