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 "ResultSetMetaD ata" 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.sho wInputDialog("P lease enter student's name");
String input2 = JOptionPane.sho wInputDialog("P lease enter student's class");
String input3 = JOptionPane.sho wInputDialog("P lease enter student's house");
conn = DriverManager.g etConnection ("jdbc:odbc:Sch ool", "", "");
set = conn.createStat ement();
set.executeUpda te("INSERT INTO tblStudent " + "VALUES ('" + input1 + " ', '" + input2 + "', '" + input3 + "')");
sql2 = "SELECT * FROM tblStudent";
rs = set.executeQuer y(sql2);
System.out.prin tln("This is the table containing the students' data:");
System.out.prin tln("------------------------------------------------");
System.out.prin tln("Student's name:" + "\t" + "Class:" + "\t\t" + "House:");
while (rs.next())
{
name = rs.getString("S tudent Name");
studentClass = rs.getString("C lass");
house = rs.getString("H ouse");
System.out.prin tln(name + "\t\t" + studentClass + "\t\t" + house);
}
conn.close();
}
catch (Exception e)
{
}
;break;
//--------------------------------------------------------------------------------
case 2: //Search by name
try
{
conn = DriverManager.g etConnection ("jdbc:odbc:Sch ool", "", "");
set = conn.createStat ement();
sql = "SELECT * FROM tblStudent";
rs = set.executeQuer y(sql);
String[] names = new String[8];
while (rs.next())
{
names[0] = "SELECT * FROM tblStudent" + "WHERE ID = 1";
System.out.prin tln(names[0]);
name = rs.getString("S tudent Name");
String input = JOptionPane.sho wInputDialog("W hich name are you searching for?");
SearchNames obj4 = new SearchNames(nam e, input);
int place = obj4.getPlace() ;
if (place == -1)
{
System.out.prin tln("\nThe name: " + input + " has not been found");
}
else
{
System.out.prin tln("\nSorry, the name: " + input + " is present");
}
}
}
catch (Exception e)
{
}
;break;
//--------------------------------------------------------------------------------------------
case 3: //Delete a name
try
{
conn = DriverManager.g etConnection ("jdbc:odbc:Sch ool", "", "");
set = conn.createStat ement();
sql = "DELETE FROM tblStudent WHERE Student Name = Thabo";
set.executeUpda te(sql);
}
catch (Exception e)
{
System.out.prin tln("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.
8 1969
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 ResultSetMetaDa ta?
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 ResultSetMetaDa ta?
Sorry about that, this is my first post - I will remember that next time thanks for pointing it out. About the ResultSetMetaDa ta, 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 ResultsSetMetaD ata interface to obtain the column names using the column numbers. You need to instantiate a new ResultSetMetaDa ta object before you can use any of the methods."
Thanks for replying to my first thread, can you please help me further?
Sorry about that, this is my first post - I will remember that next time thanks for pointing it out. About the ResultSetMetaDa ta, 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 ResultsSetMetaD ata interface to obtain the column names using the column numbers. You need to instantiate a new ResultSetMetaDa ta 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 - 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.getRe sultSetMetaData to get a ResultSetMetaDa ta object and play around with that as you like.
Do a select * from the table that you want to get column names from. (You can do a - 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.getRe sultSetMetaData to get a ResultSetMetaDa ta object and play around with that as you like.
Ok thanks a lot! That takes care of the ResultsSetMetaD ata, 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.
Ok thanks a lot! That takes care of the ResultsSetMetaD ata, 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 - catch (Exception e) {
-
e.printStackTrace();
-
}
instead which catches the exception and sprays it to the console.
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 - catch (Exception e) {
-
e.printStackTrace();
-
}
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.
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: - java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Student Name = Thabo'.
-
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
-
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
-
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
-
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
-
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate (JdbcOdbcStatement.java:288)
-
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.
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: - java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Student Name = Thabo'.
-
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
-
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
-
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
-
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
-
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate (JdbcOdbcStatement.java:288)
-
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 PreparedStateme nts and read that.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 write some PHP code which should, I think, be able to to auto create
the tables.
The SQL looks like this:
|
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 a field or two.
It would be great if there were an application out there that could either:
(a) sync all MDB designs (and/or data) designated to match one I've added
some fields/tables to
OR
(b) go into all designated MDBs and create new...
|
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
--------------------------------------------------------
2Sports 'trouble shooting
illustrated 'trouble shooting
Newsstand 'trouble shooting
|
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 you need more detail I
can be glad to prove all my points. Our goal is to make logical systems.
We don't want php,perl, or c++ making all the procedure calls and having
the host language to be checking for errors and handleing all the...
|
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 purposes). Now, the drive is defective
and can't read the tapes anymore.
Server is AIX 4.3.2 and database is IBM DB2 Server (DB2 for AIX Version
2.1.2)
| |
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
communicate with a MySQL database table on a web server, from inside
of my company's Access-VBA application. I know VBA pretty well but
have never before needed to do this HTTP/XML/MySQL type functions.
|
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 PHP script that can receive
commands via HTTP. The script receives the HTTP commands and performs
the database actions, then responds with XML.
This system already works when I test it from IE. I simply send it an
HTTP string similar to this:
|
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 based application and are faced with the choice of database software. We (at least currently) are focusing on free software. Some basic research has indicated both Postgresql and Mysql as the best of lot.
Can people please advise as to pros/cons...
|
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 reliable way of
connecting Access to a server. I am willing to switch to any version
of Access which might solve the problem. Which server would you
recommend and what are the advanatages and disadvantatges of the
server you propose? Please also inlcude...
|
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 them.
to be more clear we process this purpose we serve this to the student in the university. how??
student will send a message that contains his name,id and request by format the server want such as zaina-20024008-grade.
the grade is the request...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |