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.
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.
Comment