Java displaying data from a table based on data in other tables
I'm having some trouble figuring out how to alter my existing sql query so that it will allow me to display records from a table in my jdbc database based on data from other tables in the database. I will illustrate my problem below:
Example of "DEMANDS" table (column headers, "ID" is the primary key):
NAME|ADDRESS|DESTINATION|DATE|TIME|ID
Example of "DRIVERS" table ("REGISTRATION" is the primary key):
USERNAME|PASSWORD|REGISTRATION|NAME
Example of "JOURNEY" table ("JID" is the primary key,"REGISTRATION" is a foreign key)
JID|NAME|ADDRESS|DESTINATION|DISTANCE|REGISTRATION|DATE|TIME|STATUS
Below is the code I have that is used to display tables on a jsp file:
public String retrieve(String query) throws SQLException {
select(query);
return makeTable(rsToList());//results;
}
private void select(String query){
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
//statement.close();
}
catch(SQLException e) {
System.out.println("way way"+e);
//results = e.toString();
}
}
private String makeTable(ArrayList list) {
StringBuilder b = new StringBuilder();
String row;
b.append("<table border="3">");
for (Object s : list) {
b.append("<tr>");
row = (String) s;
for (String row1 : row) {
b.append("<td>");
b.append(row1);
b.append("</td>");
}
b.append("</tr>n");
} // for
b.append("</table>");
return b.toString();
}//makeHtmlTable
private ArrayList rsToList() throws SQLException {
ArrayList aList = new ArrayList();
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
}
aList.add(columnName);
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) {
String s = new String[cols];
for (int i = 1; i <= cols; i++) {
s[i-1] = rs.getString(i);
}
aList.add(s);
} // while
return aList;
} //rsToList
The query that I have is:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
What this query does is list drivers from the driver table that are available at the time specified in the demand (meaning their registration is not currently in a record in the journey table at the same time as the demand).
I cannot seem to figure out how I can alter the query to take into account dates as well so that the drivers that will be displayed will be ones that are available at the time and date specified in the demand as oppose to just the time (meaning their registration is not currently in a record in the journey table at the same time and date as the demand).
For example, currently if a driver in the journey table has a booking for 9:30 on 2018-11-22, they will not be displayed for a demand that has a date of 2018-11-25 at 9:30 due to the driver already having a booking at 9:30 even though they are on separate dates.
I have tried:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Date = Journey.Date
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
However, this gives me a null pointer exception error, any help on this would be greatly appreciated!
Cheers,
java mysql sql database
add a comment |
I'm having some trouble figuring out how to alter my existing sql query so that it will allow me to display records from a table in my jdbc database based on data from other tables in the database. I will illustrate my problem below:
Example of "DEMANDS" table (column headers, "ID" is the primary key):
NAME|ADDRESS|DESTINATION|DATE|TIME|ID
Example of "DRIVERS" table ("REGISTRATION" is the primary key):
USERNAME|PASSWORD|REGISTRATION|NAME
Example of "JOURNEY" table ("JID" is the primary key,"REGISTRATION" is a foreign key)
JID|NAME|ADDRESS|DESTINATION|DISTANCE|REGISTRATION|DATE|TIME|STATUS
Below is the code I have that is used to display tables on a jsp file:
public String retrieve(String query) throws SQLException {
select(query);
return makeTable(rsToList());//results;
}
private void select(String query){
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
//statement.close();
}
catch(SQLException e) {
System.out.println("way way"+e);
//results = e.toString();
}
}
private String makeTable(ArrayList list) {
StringBuilder b = new StringBuilder();
String row;
b.append("<table border="3">");
for (Object s : list) {
b.append("<tr>");
row = (String) s;
for (String row1 : row) {
b.append("<td>");
b.append(row1);
b.append("</td>");
}
b.append("</tr>n");
} // for
b.append("</table>");
return b.toString();
}//makeHtmlTable
private ArrayList rsToList() throws SQLException {
ArrayList aList = new ArrayList();
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
}
aList.add(columnName);
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) {
String s = new String[cols];
for (int i = 1; i <= cols; i++) {
s[i-1] = rs.getString(i);
}
aList.add(s);
} // while
return aList;
} //rsToList
The query that I have is:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
What this query does is list drivers from the driver table that are available at the time specified in the demand (meaning their registration is not currently in a record in the journey table at the same time as the demand).
I cannot seem to figure out how I can alter the query to take into account dates as well so that the drivers that will be displayed will be ones that are available at the time and date specified in the demand as oppose to just the time (meaning their registration is not currently in a record in the journey table at the same time and date as the demand).
For example, currently if a driver in the journey table has a booking for 9:30 on 2018-11-22, they will not be displayed for a demand that has a date of 2018-11-25 at 9:30 due to the driver already having a booking at 9:30 even though they are on separate dates.
I have tried:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Date = Journey.Date
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
However, this gives me a null pointer exception error, any help on this would be greatly appreciated!
Cheers,
java mysql sql database
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48
add a comment |
I'm having some trouble figuring out how to alter my existing sql query so that it will allow me to display records from a table in my jdbc database based on data from other tables in the database. I will illustrate my problem below:
Example of "DEMANDS" table (column headers, "ID" is the primary key):
NAME|ADDRESS|DESTINATION|DATE|TIME|ID
Example of "DRIVERS" table ("REGISTRATION" is the primary key):
USERNAME|PASSWORD|REGISTRATION|NAME
Example of "JOURNEY" table ("JID" is the primary key,"REGISTRATION" is a foreign key)
JID|NAME|ADDRESS|DESTINATION|DISTANCE|REGISTRATION|DATE|TIME|STATUS
Below is the code I have that is used to display tables on a jsp file:
public String retrieve(String query) throws SQLException {
select(query);
return makeTable(rsToList());//results;
}
private void select(String query){
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
//statement.close();
}
catch(SQLException e) {
System.out.println("way way"+e);
//results = e.toString();
}
}
private String makeTable(ArrayList list) {
StringBuilder b = new StringBuilder();
String row;
b.append("<table border="3">");
for (Object s : list) {
b.append("<tr>");
row = (String) s;
for (String row1 : row) {
b.append("<td>");
b.append(row1);
b.append("</td>");
}
b.append("</tr>n");
} // for
b.append("</table>");
return b.toString();
}//makeHtmlTable
private ArrayList rsToList() throws SQLException {
ArrayList aList = new ArrayList();
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
}
aList.add(columnName);
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) {
String s = new String[cols];
for (int i = 1; i <= cols; i++) {
s[i-1] = rs.getString(i);
}
aList.add(s);
} // while
return aList;
} //rsToList
The query that I have is:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
What this query does is list drivers from the driver table that are available at the time specified in the demand (meaning their registration is not currently in a record in the journey table at the same time as the demand).
I cannot seem to figure out how I can alter the query to take into account dates as well so that the drivers that will be displayed will be ones that are available at the time and date specified in the demand as oppose to just the time (meaning their registration is not currently in a record in the journey table at the same time and date as the demand).
For example, currently if a driver in the journey table has a booking for 9:30 on 2018-11-22, they will not be displayed for a demand that has a date of 2018-11-25 at 9:30 due to the driver already having a booking at 9:30 even though they are on separate dates.
I have tried:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Date = Journey.Date
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
However, this gives me a null pointer exception error, any help on this would be greatly appreciated!
Cheers,
java mysql sql database
I'm having some trouble figuring out how to alter my existing sql query so that it will allow me to display records from a table in my jdbc database based on data from other tables in the database. I will illustrate my problem below:
Example of "DEMANDS" table (column headers, "ID" is the primary key):
NAME|ADDRESS|DESTINATION|DATE|TIME|ID
Example of "DRIVERS" table ("REGISTRATION" is the primary key):
USERNAME|PASSWORD|REGISTRATION|NAME
Example of "JOURNEY" table ("JID" is the primary key,"REGISTRATION" is a foreign key)
JID|NAME|ADDRESS|DESTINATION|DISTANCE|REGISTRATION|DATE|TIME|STATUS
Below is the code I have that is used to display tables on a jsp file:
public String retrieve(String query) throws SQLException {
select(query);
return makeTable(rsToList());//results;
}
private void select(String query){
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
//statement.close();
}
catch(SQLException e) {
System.out.println("way way"+e);
//results = e.toString();
}
}
private String makeTable(ArrayList list) {
StringBuilder b = new StringBuilder();
String row;
b.append("<table border="3">");
for (Object s : list) {
b.append("<tr>");
row = (String) s;
for (String row1 : row) {
b.append("<td>");
b.append(row1);
b.append("</td>");
}
b.append("</tr>n");
} // for
b.append("</table>");
return b.toString();
}//makeHtmlTable
private ArrayList rsToList() throws SQLException {
ArrayList aList = new ArrayList();
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
}
aList.add(columnName);
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) {
String s = new String[cols];
for (int i = 1; i <= cols; i++) {
s[i-1] = rs.getString(i);
}
aList.add(s);
} // while
return aList;
} //rsToList
The query that I have is:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
What this query does is list drivers from the driver table that are available at the time specified in the demand (meaning their registration is not currently in a record in the journey table at the same time as the demand).
I cannot seem to figure out how I can alter the query to take into account dates as well so that the drivers that will be displayed will be ones that are available at the time and date specified in the demand as oppose to just the time (meaning their registration is not currently in a record in the journey table at the same time and date as the demand).
For example, currently if a driver in the journey table has a booking for 9:30 on 2018-11-22, they will not be displayed for a demand that has a date of 2018-11-25 at 9:30 due to the driver already having a booking at 9:30 even though they are on separate dates.
I have tried:
SELECT Drivers.Name, Drivers.Registration
FROM Drivers
LEFT JOIN Journey ON Journey.Registration = Drivers.Registration
LEFT JOIN Demands ON Demands.Date = Journey.Date
LEFT JOIN Demands ON Demands.Time = Journey.Time
WHERE Demands.id IS NULL
However, this gives me a null pointer exception error, any help on this would be greatly appreciated!
Cheers,
java mysql sql database
java mysql sql database
edited Nov 22 at 14:45
Nic3500
3,32481829
3,32481829
asked Nov 22 at 13:25
Seano989
83
83
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48
add a comment |
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432019%2fjava-displaying-data-from-a-table-based-on-data-in-other-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432019%2fjava-displaying-data-from-a-table-based-on-data-in-other-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In MySQL you can use data type "DATETIME" which includes both the date and the time in the same field. It makes it much easier to do date/time arithmetic and comparisons. Here it would solve your issue with 1 join, and some WHERE clauses to check time intervals.
– Nic3500
Nov 22 at 14:48