2013-06-07 2 views
0

MYSQLSERVER 5.1을 설치했습니다. 그런 다음 mysql-connector-java-3.0.8-stable-bin.jar을 설치하고 C : \ core 폴더의 C 드라이브에 넣습니다. 컴퓨터의 속성에서 변수 이름이 CLASSPATH이고 변수 값이 C : \ core \ mysql-connector-java-3.0.8-stable-bin.jar 인 사용자 변수.MySQL과 Java를 연결하는 방법은 무엇입니까?

지금 생성 한 데이터베이스 EMPLOYEE4 내 자바 코드는 다음과 같습니다 통신 링크 실패 : java.IO.Exception 원인 기본 :

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

class MySQLTest{ 


    public static void main(String[] args) { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection dbcon = DriverManager.getConnection( 
        "jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root"); 

     String query ="select count(*) from EMPLOYEE4 "; 
      Connection dbCon = null; 
     Statement stmt = null; 
     ResultSet rs = null; 

      //getting PreparedStatment to execute query 
      stmt = dbCon.prepareStatement(query); 

      //Resultset returned by query 
      rs = stmt.executeQuery(query); 

      while(rs.next()){ 
      int count = rs.getInt(1); 
      System.out.println("count of stock : " + count); 
      } 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
      //Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex); 
     } finally{ 
      //close connection ,stmt and resultset here 
     } 

    } 
    } 

내가 java.sql.SQLException이 그 오류 입력 스트림의 예기치 않은 끝을

+0

EMPLOYEE라는 데이터베이스가 있습니까? – Chris

+0

NetBeans, Eclipse와 같은 IDE를 사용하지 않습니까? – Lion

+4

우리에게 고함을 보내지 않아도 문제가 해결되지는 않습니다. –

답변

2

NPE가 있어야합니다. 당신은 dbcon

// initialize here 
Connection dbcon = DriverManager.getConnection( 
       "jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root"); 

String query ="select count(*) from EMPLOYEE4 "; 

// Null here 
Connection dbCon = null; 

// on dbCon which is null 
stmt = dbCon.prepareStatement(query); 

편집 코드를 같이하는 가정 방법

이에 dbCon하지에 쿼리를 실행됨에.

Connection dbcon = DriverManager.getConnection( 
        "jdbc:mysql://localhost:3306/EMPLOYEE", "root", "root"); 
String query = "select count(*) from EMPLOYEE4 "; 
Statement stmt = dbcon.createStatement(); 
ResultSet rs = stmt.executeQuery(query); 
+0

아직 동일한 예외가 있다고 생각하지 않습니다. –

+0

예 저는 데이터베이스에 사원 4가 있습니다. –

+0

@shubhamJain : 이름이'EMPLOYEE4' 인 데이터베이스가 있으면 연결 URL이 잘못되었습니다. 'jdbc : mysql : // localhost : 3306/EMPLOYEE4'와 같아야합니다. SQL에서'EMPLOYEE' 데이터베이스 내의 테이블로'EMPLOYEE4'를 참조하고 있습니다. – Lion

관련 문제