473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with Database and SQL Please

Hey guys I could really use your help with some very basic java programming. I know you programming fundis out there will find this child's play but I'm struggling with it a bit because I'm realtively new at programming.The topic is "Using a database and SQL". The exercise supplies a table called tblStudent in a database called School and requires you to perform certain actions with the table. Here are the requirements that I am having problems with:

NB: I am using JOptionPane as a GUI

Create a menu with the following functions (I've created the menu already using "switch"):
1. Insert additional records into tblStudent repeatedly until the user wishes to exit (there are 3 fields: Name, Class (A, B or C), House (Red or Blue) ). Display the table each time a student is added.
2. Search for a student by name, based on the user's input.
3. Delete a student based on their name. Before a student is to be deleted, display the student's entire details and ask the user if the displayed record is the one they wish to delete. Only delete the student if the user confirms the delete.
4. Edit a student's details: The user must be prompted to input a student's name and then the student's details must be displayed ont the screen. The user is then asked to enter new details for the student, field by field. The new details must be used to update the student's record. If the user doesn't want to change the value of a certain field, allow them to press <ENTER> - therefore only change the field's value if a new value has been entered.
5. Quit

Can you please also explain to me how to use the "ResultSetMetaData" interface to obtain the column names using the column numbers.

Here is my source code so far if needed:

case 1: //Add a student

try
{
String input1 = JOptionPane.showInputDialog("Please enter student's name");
String input2 = JOptionPane.showInputDialog("Please enter student's class");
String input3 = JOptionPane.showInputDialog("Please enter student's house");
conn = DriverManager.getConnection ("jdbc:odbc:School", "", "");
set = conn.createStatement();
set.executeUpdate("INSERT INTO tblStudent " + "VALUES ('" + input1 + " ', '" + input2 + "', '" + input3 + "')");
sql2 = "SELECT * FROM tblStudent";
rs = set.executeQuery(sql2);

System.out.println("This is the table containing the students' data:");
System.out.println("------------------------------------------------");
System.out.println("Student's name:" + "\t" + "Class:" + "\t\t" + "House:");

while (rs.next())
{
name = rs.getString("Student Name");
studentClass = rs.getString("Class");
house = rs.getString("House");
System.out.println(name + "\t\t" + studentClass + "\t\t" + house);
}
conn.close();
}

catch (Exception e)
{
}
;break;
//--------------------------------------------------------------------------------

case 2: //Search by name
try
{
conn = DriverManager.getConnection ("jdbc:odbc:School", "", "");
set = conn.createStatement();
sql = "SELECT * FROM tblStudent";
rs = set.executeQuery(sql);
String[] names = new String[8];

while (rs.next())
{
names[0] = "SELECT * FROM tblStudent" + "WHERE ID = 1";
System.out.println(names[0]);
name = rs.getString("Student Name");
String input = JOptionPane.showInputDialog("Which name are you searching for?");
SearchNames obj4 = new SearchNames(name, input);
int place = obj4.getPlace();

if (place == -1)
{
System.out.println("\nThe name: " + input + " has not been found");
}
else
{
System.out.println("\nSorry, the name: " + input + " is present");
}
}
}
catch (Exception e)
{
}
;break;
//--------------------------------------------------------------------------------------------

case 3: //Delete a name

try
{
conn = DriverManager.getConnection ("jdbc:odbc:School", "", "");
set = conn.createStatement();
sql = "DELETE FROM tblStudent WHERE Student Name = Thabo";
set.executeUpdate(sql);
}
catch (Exception e)
{
System.out.println("Sorry, there is an error");
}
; break;
//---------------------------------------------------------------------------------------------

All help will be greatly appreciated as knowing how to do this is crucial for my upcoming final exams (I'm in high school). I can provide my email address at request if needed. Thank you very much in advance.
Sep 26 '07 #1
8 1939
r035198x
13,262 8TB
1.) Use code tags if you have to post code.
2.) Only post the code that is relevant to a specific problem not to dump the whole lot.
3.) Did you open the API documentation page for ResultSetMetaData?
Sep 26 '07 #2
1.) Use code tags if you have to post code.
2.) Only post the code that is relevant to a specific problem not to dump the whole lot.
3.) Did you open the API documentation page for ResultSetMetaData?

Sorry about that, this is my first post - I will remember that next time thanks for pointing it out. About the ResultSetMetaData, the problem is I'm not sure how to apply it in the context of the program. This is what I am supposed to do:

"Change the displayCD() method so that it uses the ResultsSetMetaData interface to obtain the column names using the column numbers. You need to instantiate a new ResultSetMetaData object before you can use any of the methods."

Thanks for replying to my first thread, can you please help me further?
Sep 26 '07 #3
r035198x
13,262 8TB
Sorry about that, this is my first post - I will remember that next time thanks for pointing it out. About the ResultSetMetaData, the problem is I'm not sure how to apply it in the context of the program. This is what I am supposed to do:

"Change the displayCD() method so that it uses the ResultsSetMetaData interface to obtain the column names using the column numbers. You need to instantiate a new ResultSetMetaData object before you can use any of the methods."

Thanks for replying to my first thread, can you please help me further?
Do a select * from the table that you want to get column names from. (You can do a
Expand|Select|Wrap|Line Numbers
  1. select * from tableName where 1 = 2
because you are not really worried about the data)
Execute that sql and store the results in ResultSet. Then call the ResultSet.getResultSetMetaData to get a ResultSetMetaData object and play around with that as you like.
Sep 26 '07 #4
Do a select * from the table that you want to get column names from. (You can do a
Expand|Select|Wrap|Line Numbers
  1. select * from tableName where 1 = 2
because you are not really worried about the data)
Execute that sql and store the results in ResultSet. Then call the ResultSet.getResultSetMetaData to get a ResultSetMetaData object and play around with that as you like.
Ok thanks a lot! That takes care of the ResultsSetMetaData, but what about the other things like inserting, searching, deleting and editing? I tried, as you can see from my source code, to use the sql commands the way I learned, but when I run each of the portions it doesn't work (e.g. I select "Insert a student" from the menu and it prompts the user to input the name, class and house, but then nothing happens. What's wrong with my code?

Thanks again in advance.
Sep 26 '07 #5
r035198x
13,262 8TB
Ok thanks a lot! That takes care of the ResultsSetMetaData, but what about the other things like inserting, searching, deleting and editing? I tried, as you can see from my source code, to use the sql commands the way I learned, but when I run each of the portions it doesn't work (e.g. I select "Insert a student" from the menu and it prompts the user to input the name, class and house, but then nothing happens. What's wrong with my code?

Thanks again in advance.
Perhaps an Exception is being thrown but you would never know that because you are gobbling up all the exceptions with your catch blocks which look like this
catch (Exception e)
{
}
You should use
Expand|Select|Wrap|Line Numbers
  1.  catch (Exception e) {
  2.      e.printStackTrace();
  3. }
instead which catches the exception and sprays it to the console.
Sep 26 '07 #6
Perhaps an Exception is being thrown but you would never know that because you are gobbling up all the exceptions with your catch blocks which look like this


You should use
Expand|Select|Wrap|Line Numbers
  1.  catch (Exception e) {
  2.      e.printStackTrace();
  3. }
instead which catches the exception and sprays it to the console.
Ok I'm going to try that right now and get back to you.
Sep 26 '07 #7
I tried what you suggested and it helped me to solve the problems with the Insert method and showed what's wrong with the Delete method - I copied the following from the console so that you can take a look and identify what I need to change, because I'm not sure:

Expand|Select|Wrap|Line Numbers
  1. java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Student Name = Thabo'. 
  2.   at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
  3.   at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
  4.   at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
  5.   at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
  6.   at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate  (JdbcOdbcStatement.java:288)
  7.   at UseSchool.main(UseSchool.java:260)
As for the Insert method, the console still doesn't show me anything. At least I'm getting progress thanks to you.
Sep 26 '07 #8
r035198x
13,262 8TB
I tried what you suggested and it helped me to solve the problems with the Insert method and showed what's wrong with the Delete method - I copied the following from the console so that you can take a look and identify what I need to change, because I'm not sure:

Expand|Select|Wrap|Line Numbers
  1. java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Student Name = Thabo'. 
  2.   at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
  3.   at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
  4.   at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
  5.   at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
  6.   at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate  (JdbcOdbcStatement.java:288)
  7.   at UseSchool.main(UseSchool.java:260)
As for the Insert method, the console still doesn't show me anything. At least I'm getting progress thanks to you.
Get a tutorial on how to use PreparedStatements and read that.
Sep 26 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
39
by: Scotter | last post by:
Okay I think my title line was worded misleadingly. So here goes again. I've got quite 20 identical MDB files running on an IIS5 server. From time to time I need to go into various tables and add...
20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
5
by: HSP | last post by:
hi. i need to restore an old database. The db was backed up using a DLT drive, using 2 volumes. The content for the tapes was copied to file onto Solaris machine using rsh and dd (for backup...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: Charles Wilson | last post by:
Can someone point me to some good instructions on using the WinInet.dll functions from VBA? I need to interact with a MySQL database via HTTP strings. We already have the database set up and a...
1
by: | last post by:
Hi Database Gurus, Not to start a war among fanatics, but I just wanted to get honest opinion/advise of smart folks like you about this. We are about to begin development for a data intensive web...
21
by: nihad.nasim | last post by:
Hi there, I have a database in Access that I need on the web. The web page should connect to the database and write records for certain tables and view records for others. I want to know a...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.