2012-03-31 4 views
1

나는 java의 초보자입니다. Eclipse에서 커넥터 j 플러그인을 구현하는 Java 프로그램을 수행 중입니다. 내가 그 설치를 마친 후에. 나는 데이터베이스 연결을 테스트했다. 다음 코드는 테스트 연결에 사용됩니다. 내가 실행할 때MySQL 커넥터 j 오류

import javax.swing.JOptionPane; 
import java.sql.*; 
public class JDBCProgram{ 

    static String userid="root", password = "devkbsc"; 
    static String url = "jdbc:mysql://localhost:3306/nejagar"; 
    // String url = "jdbc:mysql://localhost:3306/nejagar"; ? 
    static Statement stmt; 
    static Connection con; 
    public static void main(String args[]){ 
     JOptionPane.showMessageDialog(null,"JDBC Programming showing Creation of Table's"); 
     int choice = -1; 

     do{ 
      choice = getChoice(); 
      if (choice != 0){ 
       getSelected(choice); 
      } 
     } 
     while (choice != 0); 
      System.exit(0); 
    } 

    public static int getChoice() 
    { 
     String choice; 
     int ch; 
     choice = JOptionPane.showInputDialog(null, 
      "1. Create Employees Table\n"+ 
      "2. Create Products Table\n"+ 
      "0. Exit\n\n"+ 
      "Enter your choice"); 
     ch = Integer.parseInt(choice); 
     return ch; 

    } 

    public static void getSelected(int choice){ 
     if(choice==1){ 
      createEmployees(); 
     } 
     if(choice==2){ 
      createOrders(); 
     } 
    } 

    public static Connection getConnection() 
    { 



     try { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Class.forName("myDriver.ClassName"); ? 


     } catch(java.lang.ClassNotFoundException e) { 
      System.err.print("ClassNotFoundException: "); 
      System.err.println(e.getMessage()); 
     } 

     try { 
      con = DriverManager.getConnection(url, 
       userid, password); 

     } catch(SQLException ex) { 
      System.err.println("SQLException: " + ex.getMessage()); 
     } 

     return con; 
    } 

    /*CREATE TABLE Employees (
      Employee_ID INTEGER, 
      Name VARCHAR(30) 
     );*/ 
    public static void createEmployees() 
    { 
     Connection con = getConnection(); 

     String createString; 
     createString = "create table Employees (" + 
          "Employee_ID INTEGER, " + 
          "Name VARCHAR(30))"; 
     try { 
      stmt = con.createStatement(); 
      stmt.executeUpdate(createString); 
      stmt.close(); 
      con.close(); 

     } catch(SQLException ex) { 
      System.err.println("SQLException: " + ex.getMessage()); 
     }JOptionPane.showMessageDialog(null,"Employees Table Created"); 
    } 

    /*CREATE TABLE Orders (
      Prod_ID INTEGER, 
      ProductName VARCHAR(20), 
      Employee_ID INTEGER 
     );*/ 

    public static void createOrders() 
    { 
     Connection con = getConnection(); 

     String createString; 
     createString = "create table Orders (" + 
          "Prod_ID INTEGER, " + 
          "ProductName VARCHAR(20), "+ 
          "Employee_ID INTEGER)"; 


     try { 
      stmt = con.createStatement(); 
      stmt.executeUpdate(createString); 

      stmt.close(); 
      con.close(); 

     } catch(SQLException ex) { 
      System.err.println("SQLException: " + ex.getMessage()); 
     } 
     JOptionPane.showMessageDialog(null,"Orders Table Created"); 
    } 

은 내가 잘못했다 어디 모르겠어, 정말 콘솔

ConnectionProperties> 
<PropertyCategory name="Connection/Authentication"> 
    <Property name="user" required="No" default="" sortOrder="-2147483647" since="all versions"> 
    The user to connect as 
    </Property> 
    <Property name="password" required="No" default="" sortOrder="-2147483646" since="all versions"> 
    The password to use when connecting 
    </Property> 

에이를 발견했다. 당신은 또한 그 보장해야합니다, 당신이 아직하지 않은 경우

Class.forName("com.mysql.jdbc.Driver"); 

와 라인

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

를 교체해야합니다,

답변

2

는 MySQL의 커넥터/J를 사용하려면 도와주세요 mysql-connector-java-5.1.18-bin.jar과 같은 이름을 가진 MySQL Connector/J jar는 프로젝트의 빌드 경로에있다. 그렇지 않으면 ClassNotFoundException이 발생한다.