Java 使用代码建立与 jdbc 的连接
Posted
技术标签:
【中文标题】Java 使用代码建立与 jdbc 的连接【英文标题】:Java establish connection to jdbc with code 【发布时间】:2014-09-23 10:47:36 【问题描述】:您好,我正在努力建立与我的 jdbc 数据库的自动连接。当我运行程序时,它会给我这个错误:java.sql.SQLNonTransientConnectionException:java.net.ConnectException:Error connecting to server localhost on port 1527 with message Connection refused: connect.
当我手动连接到我的 jdbc 驱动程序时,程序运行良好。我想知道是否有一种方法可以使用代码而不是手动连接到驱动程序。
这是我的一些代码:
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();
我怀疑我可能不得不在我的 try 块中添加另一个语句。 当我调试我的程序时,问题在于以下语句:
Connection conn = DriverManager.getConnection(createDB);
当程序应该执行该语句时,它会进入 catch 并抛出错误消息。
【问题讨论】:
请发布错误信息.. 看起来您的数据库服务没有运行。或在另一个端口上运行。 您需要提供用户名和密码 String createDB = "jdbc:derby://localhost:1527/C:/211092207/HotelBookings;create=true";类似于 (user=USERNAME;password=PASSWORD;) 【参考方案1】:错误指示“连接被拒绝”。将代码测试减少到只是连接,直到成功。 用户名和密码显然没有设置在任何地方,所以从那里开始。有关设置用户和密码的位置,请参阅连接字符串和选项的 Derby 示例。建议不要硬编码密码,而是通过连接参数发送,取自操作员作为控制台参数。
【讨论】:
【参考方案2】:您应该遵循 JDBC 中的 7 个步骤,
1)加载驱动
2)定义连接URL
3)建立连接
4)创建语句对象
5)执行查询
6)处理结果
7)关闭连接
您没有在任何地方加载课程。 对于 derby 数据库,我的猜测是
Class.forName("org.apache.derby.jdbc.ClientDriver");
此外,您刚刚创建了语句,您需要编写所需的查询,然后也执行它。记得关闭。
【讨论】:
以上是关于Java 使用代码建立与 jdbc 的连接的主要内容,如果未能解决你的问题,请参考以下文章