2013-06-18 351 views
1

我用我下面的代碼在Eclipse中使用Derby數據庫,但得到錯誤的連接錯誤

Insufficient data while reading from the network - expected a minimum of 6 bytes and received only 0 bytes. The connection has been terminated. 
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) 
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) 
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source) 
    at java.sql.DriverManager.getConnection(DriverManager.java:322) 
    at java.sql.DriverManager.getConnection(DriverManager.java:273) 
    at jdbc.JDBCSample.main(JDBCSample.java:19)."  

包JDBC;

import java.sql.*; 

public class JDBCSample { 

public static void main(String args[]) { 

String connectionURL = "jdbc:derby://127.0.0.1:8080/SAMPLE"; 
// Change the connection string according to your db, ip, username and password 

try { 

    // Load the Driver class. 
    Class.forName("org.apache.derby.jdbc.ClientDriver"); 
    // If you are using any other database then load the right driver here. 

    //Create the connection using the static getConnection method 
    Connection con = DriverManager.getConnection (connectionURL); 

    //Create a Statement class to execute the SQL statement 
    Statement stmt = con.createStatement(); 

    //Execute the SQL statement and get the results in a Resultset 
    ResultSet rs = stmt.executeQuery("select moviename, releasedate from movies"); 


    // Iterate through the ResultSet, displaying two values 
    // for each row using the getString method 

    while (rs.next()) 
     System.out.println("Name= " + rs.getString("moviename") + " Date= " + rs.getString("releasedate")); 
    con.close(); 

} 
catch (SQLException e) { 
    e.printStackTrace(); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 
finally { 


} 
} 
} 
+0

歡迎來到StackOverflow。你能告訴我們在哪一行出現'Exception',並且發佈堆棧跟蹤嗎? –

+0

java.sql.SQLNonTransientConnectionException:從網絡讀取數據時數據不足 - 預計最少6個字節,只接收0個字節。連接已終止。 \t在org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(未知來源) \t在org.apache.derby.client.am.SqlException.getSQLException(未知來源) \t在org.apache.derby.jdbc .ClientDriver.connect(未知來源) \t在java.sql.DriverManager.getConnection(DriverManager.java:322) \t在java.sql.DriverManager.getConnection(DriverManager.java:273) \t在jdbc.JDBCSample.main (JDBCSample.java:19) –

+0

m運行時錯誤..... –

回答

0

我覺得你的問題將得到解決,如果你 通話getConnection("..", "..", "..")方法使用用戶名和密碼。 示例

Connection con = DriverManager.getConnection(connectionURL, "sa", "sa");