2014-09-23 168 views
-1

你好,我在啓動我的數據庫時遇到了問題。你能指出我的問題在哪裏,比事先提前。JDBC建立與數據庫的連接

錯誤消息,我得到:java.sql.SQLNonTransientConnectionException:java.net.ConnectException:錯誤連接服務器本地主機上=端口消息連接拒絕:連接。

這裏是我的代碼:

public void CreateDatabase() 
    { 

    String createDB = "jdbc:derby://localhost:1527/C:/211092207/HotelBookings;create=true"; 
    String createCustomer = "CREATE TABLE CUSTOMER (CUSTOMERID NUMERIC(15) NOT NULL PRIMARY KEY, LASTNAME VARCHAR(20), FIRSTNAME VARCHAR(20), GENDER CHAR(1), EMAIL VARCHAR(50), CREDITRATING VARCHAR(9), NATIONALITY VARCHAR(20), DATEOFBIRTH VARCHAR(20), PHONE NUMERIC(10), CELLPHONE NUMERIC(10))"; 
    String createRoom = "CREATE TABLE ROOM (ROOMNO NUMERIC(5) PRIMARY KEY, ROOMTYPE VARCHAR(9), DAILYRATE NUMERIC(20), STATUS CHAR(1))"; 
    String createBooking = "CREATE TABLE BOOKING (BOOKINGID NUMERIC(10) NOT NULL PRIMARY KEY, ROOMNO NUMERIC(5), PROPOSEDCHECKINDATE DATE, PROPOSEDCHECKOUTDATE DATE, CHECKEDIN CHAR(1), ACTUALCHECKINDATE DATE, CHECKEDOUT CHAR(1), ACTUALCHECKOUTDATE DATE, CANCELLED CHAR(1), CANCELDATE DATE, CANCELREASON VARCHAR(100),AMOUNT NUMERIC(20), PAYDATE DATE, PAYMODE VARCHAR(15), STATUS VARCHAR(50), PAID CHAR(1), DATE DATE, CUSTOMERID NUMERIC(15))"; 
    String refOne = "ALTER TABLE BOOKING ADD FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER(CUSTOMERID)"; 
    String refTwo = "ALTER TABLE BOOKING ADD FOREIGN KEY(BOOKINGID) REFERENCES BOOKING(BOOKINGID)"; 

    Date one = new Date(20/9/2014); 
    Date two = new Date(10/10/1010); 
    Date three = new Date(16/9/2014); 
    Booking book = new Booking(); 
    book.SetBookingID("9879974564"); 
    book.SetRoomNumber("001"); 
    book.SetProposedCheckInDate(one); 
    book.SetProposedCheckOutDate(one); 
    book.SetCheckedIn('F'); 
    book.SetActualCheckInDate(one); 
    book.SetActualCheckOutDate(one); 
    book.setCancelled('F'); 
    book.SetCancelDate(two); 
    book.SetCancelReason(""); 
    book.SetAmount(10.20); 
    book.SetPayDate(one); 
    book.SetPayMode("Bank Deposit"); 
    book.SetStatus("Done"); 
    book.SetPaid('T'); 
    book.SetDate(three); 
    book.SetCustomerID("9112315190086"); 

    String insertStatement = ("INSERT INTO ROOM(ROOMNO, ROOMTYPE, DAILYRATE, STATUS) VALUES (001, 'Single', 120.00, 'A')"); 
    String insertStatementTwo = ("INSERT INTO ROOM(ROOMNO, ROOMTYPE, DAILYRATE, STATUS) VALUES (002, 'Single', 120.00, 'A')"); 
    String insertStatementThree = ("INSERT INTO ROOM(ROOMNO, ROOMTYPE, DAILYRATE, STATUS) VALUES (003, 'Double', 230.00, 'A')"); 

    String insertStatementFour = ("INSERT INTO CUSTOMER(CUSTOMERID, LASTNAME, FIRSTNAME, GENDER, EMAIL, CREDITRATING, NATIONALITY, DATEOFBIRTH, PHONE, CELLPHONE) " + 
      "VALUES (9112415190086, 'Nel', 'Piet', 'M', '[email protected]', 'Good', 'America', '31/12/2014', 0721360363, 0721589859)"); 

    String insertStatementFive = ("INSERT INTO CUSTOMER(CUSTOMERID, LASTNAME, FIRSTNAME, GENDER, EMAIL, CREDITRATING, NATIONALITY, DATEOFBIRTH, PHONE, CELLPHONE) " + 
      "VALUES (9112315190086, 'van Tonder', 'Hannes', 'M', '[email protected]', 'Good', 'South Africa', '31/12/2014', 0711360193, 0791589789)"); 

    String insertStatementSix = ("INSERT INTO CUSTOMER(CUSTOMERID, LASTNAME, FIRSTNAME, GENDER, EMAIL, CREDITRATING, NATIONALITY, DATEOFBIRTH, PHONE, CELLPHONE) " + 
      "VALUES (9115987489968, 'le Roux', 'Paul', 'M', '[email protected]', 'Good', 'South Africa', '31/12/2014', 0721361193, 0821589789)"); 

    try 
    { 
     try { 

      Class.forName("org.apache.derby.jdbc.ClientDriver"); 
     } catch (ClassNotFoundException ex) { 
      JOptionPane.showMessageDialog(null, "not working"); 
     } 

當我調試我的代碼下面的語句使編譯器去拋出一個錯誤catch語句

Connection conn = DriverManager.getConnection(createDB); 

     Statement stmt = conn.createStatement(); 
     try 
     { 
     //Driver = new Driver(); 
     //java.sql.Driver(createDB); 
     } 
     catch(Exception ex) 
     { 

     } 
     try 
     { 
      stmt.executeUpdate(createCustomer); 
     } //End try 
     catch(Exception ex) 
     { 
     } //End catch 
     try 
     { 
      stmt.executeUpdate(createRoom); 
     } //End try 
     catch(Exception ex) 
     { 
     } //End try 
     try 
     { 
      stmt.executeUpdate(createBooking); 
     } //End try 
     catch(Exception ex) 
     { 
     } //Catch 
     try 
     { 
      stmt.executeUpdate(refOne); 
     } //End try 
     catch(Exception ex) 
     { 
      JOptionPane.showMessageDialog(null, "Failed"); 
     } //End catch 

     try 
     { 
      stmt.executeUpdate(refTwo); 
     } //End try 
     catch(Exception ex) 
     { 
      JOptionPane.showMessageDialog(null, "Failed"); 
     } //End catch 
     try 
     { 
     stmt.executeUpdate(insertStatement); 
     } 
     catch(Exception ex) 
     { 
     } 
     try 
     { 
     stmt.executeUpdate(insertStatementTwo); 
     } 
     catch(Exception ex) 
     { 
     } 
     try 
     { 
     stmt.executeUpdate(insertStatementThree); 
     } 
     catch(Exception ex) 
     { 

     } 
     try 
     { 
     stmt.executeUpdate(insertStatementFour); 
     } 
     catch(Exception ex) 
     { 
     } 
     try 
     { 
     stmt.executeUpdate(insertStatementFive); 
     } 
     catch(Exception ex) 
     { 
     } 
     try 
     { 
     stmt.executeUpdate(insertStatementSix); 
     } 
     catch(Exception ex) 
     { 
      //JOptionPane.showMessageDialog(null, ex); 
     } 
     conn.close(); 
    } //End try 
    catch(SQLException ex) 
    { 
    JOptionPane.showMessageDialog(null, ex); 
    } //End catch 
} 
+1

java.sql.SQLNonTransientConnectionException:java.net.ConnectException:錯誤連接到server localhost on =端口1527,並顯示消息Connection refused:connect。你的連接被拒絕。因此您的計算機無法找到主機,您的憑據可能是錯誤的(用戶名/密碼)或端口號可能錯誤,或者文件路徑可能是錯誤的 – 2014-09-23 13:11:06

+0

重複[Java建立與代碼的jdbc連接]( http://stackoverflow.com/questions/25993151/java-establish-connection-to-jdbc-with-code) – 2014-09-23 16:13:10

+0

不要轉發您的問題。 – Raedwald 2014-09-23 21:50:53

回答

0

最有可能的就是變化你的連接網址。

更換

String createDB = "jdbc:derby://localhost:1527/C:/21109220/HotelBookings;create=true"; 

String createDB = "jdbc:derby://localhost:1527/HotelBookings;create=true"; 

String createDB = "jdbc:derby://localhost:1527/HotelBookings"; 

然後,它應該沒問題。