Java使用代码建立与jdbc的连接

时间:2021-12-17 18:53:41

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.

你好,我正在建立一个自动连接到我的jdbc数据库。当我运行该程序时它会给我这个错误:java.sql.SQLNonTransientConnectionException:java.net.ConnectException:在端口1527上连接到服务器localhost时出错,并显示消息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.

当我手动连接到我的jdbc驱动程序时,程序运行正常。我想知道是否有一种方法可以使用代码连接到驱动程序而不是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:

我怀疑maby我必须在我的try块中添加另一个语句。当我调试我的程序时,问题出在这个声明:

Connection conn = DriverManager.getConnection(createDB);

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

当程序执行该语句时,它会转到catch并抛出错误消息。

2 个解决方案

#1


0  

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.

该错误表示“拒绝连接”。减少代码测试,直到连接成功为止。用户名和密码显然没有设置在任何地方,所以从那开始。请参阅Derby连接字符串示例以及设置用户和pw的位置选项。建议不要硬编码密码,而是通过连接参数发送,从操作员作为控制台参数。

#2


0  

You should follow 7 steps in JDBC,

您应该遵循JDBC中的7个步骤,

1)Load the driver

1)加载驱动程序

2)Define the Connection URL

2)定义连接URL

3)Establish the Connection

3)建立连接

4)Create a Statement Object

4)创建一个Statement对象

5)Execute a Query

5)执行查询

6)Process the results

6)处理结果

7)close the Connection

7)关闭连接

you are not loading the class anywhere. For derby database my guess is

你没有在任何地方加载课程。对于德比数据库,我的猜测是

   Class.forName("org.apache.derby.jdbc.ClientDriver");

Moreover you have just created the statement, you need to write your required query and then execute it too. remember to close.

此外,您刚刚创建了语句,您需要编写所需的查询,然后执行它。记得关闭。

#1


0  

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.

该错误表示“拒绝连接”。减少代码测试,直到连接成功为止。用户名和密码显然没有设置在任何地方,所以从那开始。请参阅Derby连接字符串示例以及设置用户和pw的位置选项。建议不要硬编码密码,而是通过连接参数发送,从操作员作为控制台参数。

#2


0  

You should follow 7 steps in JDBC,

您应该遵循JDBC中的7个步骤,

1)Load the driver

1)加载驱动程序

2)Define the Connection URL

2)定义连接URL

3)Establish the Connection

3)建立连接

4)Create a Statement Object

4)创建一个Statement对象

5)Execute a Query

5)执行查询

6)Process the results

6)处理结果

7)close the Connection

7)关闭连接

you are not loading the class anywhere. For derby database my guess is

你没有在任何地方加载课程。对于德比数据库,我的猜测是

   Class.forName("org.apache.derby.jdbc.ClientDriver");

Moreover you have just created the statement, you need to write your required query and then execute it too. remember to close.

此外,您刚刚创建了语句,您需要编写所需的查询,然后执行它。记得关闭。