2017-11-29 7 views
0

結果セットをデータ・フィールドをマップするオブジェクトの配列に変換しようとしていますが、オープンするように呼び出してもdb接続エラーが発生します? CRM.SQLServer.DisconnectDB()を呼び出さない場合は、それは正しく動作します。どうやら詳細を追加する必要があるので、今すぐ詳細を入力するように入力しています。結果がオブジェクト配列にキャストされたときにJavaデータベースが接続されていない

接続エラー:

java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed. 
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:710) 
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.checkClosed(SQLServerStatement.java:1071) 
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:387) 
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:1009) 
    at main.DataClasses.Companies.ConvertToArrayList(Companies.java:51) 
    at main.CRM.main(CRM.java:35) 
    ... 11 more 
Exception running application main.CRM 

メインクラス:

package main; 

import java.sql.SQLException; 
import java.util.ArrayList; 

import main.DataClasses.Companies; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class CRM extends Application { 

    public static SQLServer SQLServer = new SQLServer(); 
    public static Users Users = new Users(); 
    public static Navigation Navigation = new Navigation(); 
    public static Companies Companies = new Companies(); 

    public static String Verision ="0.0.1"; 

    @Override 
    public void start (Stage primaryStage) throws Exception 
    { 
    Parent root = FXMLLoader.load(getClass().getResource("UI/Login.fxml")); 
    primaryStage.setTitle("Basic CRM Verision:"+Verision); 
    primaryStage.setScene(new Scene(root, 600,400)); 
    primaryStage.show(); 
    } 

    public static void main(String[] args) throws SQLException { 
     launch(args); 

     ArrayList<Companies> result = Companies.ConvertToArrayList(Companies.SelectTopCompanies(10)); 
     for(Companies temp:result) { 
      System.out.println(temp.getCompanyName()); 
     } 

    } 

} 

企業クラス:

package main.DataClasses; 

import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import main.CRM; 

public class Companies { 

private int mID; 
private String mCompanyName; 
private String mCity; 
private int mZipCode; 
private String mState; 

public Companies() {} 
public Companies(int ID, String CompanyName, String City, int ZipCode, String State) throws SQLException { 
    mID = ID; 
    mCompanyName = CompanyName; 
    mCity = City; 
    mZipCode = ZipCode; 
    mState = State; 
} 
public String getCompanyName() { 
    return mCompanyName; 
} 
public void CreateCompany(String CompanyName, String City, int ZipCode, String State) throws SQLException { 
    String insertTableSQL = "INSERT INTO Company" + "(CompanyName, City, ZipCode, State) VALUES" + "(?,?,?,?)"; 
    PreparedStatement preparedStatement = CRM.SQLServer.conn.prepareStatement(insertTableSQL); 
    preparedStatement.setString(1, CompanyName); 
    preparedStatement.setString(2, City); 
    preparedStatement.setInt(3, ZipCode); 
    preparedStatement.setString(4, State); 
    CRM.SQLServer.ConnectToDB(); 
    preparedStatement.executeUpdate(); 
    CRM.SQLServer.DisconnectDB(); 
} 
public ResultSet SelectTopCompanies(int amount) throws SQLException { 
    CRM.SQLServer.ConnectToDB(); 
    PreparedStatement statement = CRM.SQLServer.conn.prepareStatement("SELECT TOP " + amount + " * FROM Company"); 
    ResultSet CompanyResultSet = statement.executeQuery(); 
    CRM.SQLServer.DisconnectDB(); 
    return CompanyResultSet; 

} 

public ArrayList <Companies> ConvertToArrayList(ResultSet resultset) throws SQLException { 
    CRM.SQLServer.ConnectToDB(); 
    ArrayList <Companies> result = new ArrayList <Companies>(); 
    while (resultset.next()) { 
    result.add(new Companies(resultset.getInt("ID"), resultset.getString("CompanyName"), 
    resultset.getString("City"), resultset.getInt("ZipCode"), resultset.getString("State"))); 
    } 
    CRM.SQLServer.DisconnectDB(); 
    return result; 
} 

} 

答えて

0

私は股関節を推測接続はあなたのResultSetに依存しているため、接続していません。

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

あなたはそう簡単な解決策は、これを持っているあなたの方法を「マージ」することです一度

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options

あなたたresultSetを通過することができます。

public ArrayList <Companies> SelectTopCompanies(int amount) throws SQLException {...} 
+0

私がやったことだ、ありがとう! –

+0

あなたは大歓迎です:) –

関連する問題