您好我在启动数据库时遇到问题。能不能提前告诉我问题在哪里,而不是提前。
我得到的错误消息: java.sql.SQLNonTransientConnectionException:java.net.ConnectException:错误 正在连接 到 服务器 localhost on = 端口 1527 消息< / strong> 连接 拒绝: 连接。
这是我的代码:
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', '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
{
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
}
答案 0 :(得分: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";
然后应该没问题。