2016-06-21 2 views
-3

mysqlconnector으로 쿼리를 실행하고 결과를 ResulSet과 함께 가져 오면 내 첫 번째 코드는 항상 null입니다.Java에서 문자열 연결은 첫 번째 결과와 함께 null을 나타냅니다.

이 내 코드 :

Class.forName("com.mysql.jdbc.Driver"); 
    conn =DriverManager.getConnection("jdbc:mysql://localhost/","",""); 
    String query = "SELECT Category,Account FROM Settings" ;// 
    PreparedStatement select = conn.prepareStatement(query); 
    ResultSet rs = select.executeQuery(); 

    while (rs.next()) 
     { 

       category += rs.getString("Category")+","; 
       account += rs.getString("Account")+","; 

그리고 내 결과가 항상 :

nullresult, result, result... 심지어는 하나 개의 결과 만은 항상 때 nullResult

내가이 문제를 해결할 수있는 방법?

+0

전체 방법을 제공하고 무엇이라도 말할 수 있습니다! –

답변

3

당신이 선언 할 수 null

내 가정은 당신이 String category =null;

변화는 당신이 String category가 자동으로 또는 명시 적으로 (코드) null으로 지정된 선언 한 String category ="";

0

변수이다 가지고있다.

변수 category"" (빈 문자열)로 지정하는 것이 좋습니다. 그 뜻은 당신이 기대하는 어떤 결과를 제공합니다 .. 여기

샘플 코드는 Demo To Demonstrate Your Problem

String category = ""; 
categorty += "Appeltants Ronald"; 
System.out.prinln(category); //it will print Appeltants Ronald 

당신을 감사합니다.

-2

conn =DriverManager.getConnection("jdbc:mysql://localhost/","",""); 문에는 데이터베이스 이름을 언급하지 않았으므로 username,password을 사용하여 데이터베이스에 액세스 할 수 있습니다.

그래서 데이터베이스와의 연결이 없으므로 null이 반환됩니다.