Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello I am struggeling to establish an automatic connection to my jdbc database. When ever I run the program it gives me this error: java.sql.SQLNonTransientConnectionException:java.net.ConnectException:Error connecting to server localhost on port 1527 with message Connection refused: connect.

When I manually connect to my jdbc driver the program works fine. I would like to know if there is a way that I can connect to the driver using code instead of doing it mannualy.

Here is some of my code:

 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', 'pietnel@hotmail.com', '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', 'hannesvantonder@gmail.com', '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', 'paul@gmail.com', 'Good', 'South Africa', '31/12/2014', 0721361193, 0821589789)");
try
{
Connection conn = DriverManager.getConnection(createDB);
            Statement stmt = conn.createStatement();

}
catch(Exception ex)
{

}
conn.close();

I suspect that maby I would have to add another statement in my try block. When I debug my program the problem lies at this statement:

Connection conn = DriverManager.getConnection(createDB);

when the program should execute that statement it goes to the catch and throw the error message.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
167 views
Welcome To Ask or Share your Answers For Others

1 Answer

The error indicates "Connection Refused". Reduce code test to just connecting until that is successful. The username and password are not apparently set anywhere so start with that. See Derby examples of connect string and options for where to set the user and pw. Recommend not hard coding password but rather send it in via connection parameter, taken from operator as console argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...