📄 student.java
字号:
void verifyLogin() //Verifing Login for username 'administrator' and password 'password'
{
try
{
String val1 = tlogname.getText();
val1 = val1.trim();
String val2 = (String)tlogpass.getText();
val2 = val2.trim();
//JDBC step 1 : Writing the query
query1 = "SELECT * FROM login WHERE login='"+val1+"' AND passwd='"+val2+"'";
//JDBC step 2 : Registering the driver="sun.jdbc.odbc.JdbcOdbcDriver"; for MSACCESS
Class.forName(driver);
//JDBC step 3 : Using the driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con=DriverManager.getConnection("jdbc:odbc:student");
//JDBC step 4 : Creating a statement
Statement st = con.createStatement();
//JDBC step 5 : Exceuting the query
boolean bresults = st.execute(query1);
//JDBC step 6 : Checking the results if(bresults)
{
ResultSet result = st.getResultSet();
//JDBC step 7 : if there are results retrieve the SET
if(result!=null)
{
//Calling the method "showResults(ResultSet r)" and pass ResultSet to it
showResults(result);
}
else
{
}
//JDBC step 8 : Closing the database connection
//con.close();
}
}
catch(SQLException ex)
{
System.out.println("Unable to access the database");
}
catch(ClassNotFoundException ex)
{
System.out.println("Class not found");
}
catch(Exception ex)
{
System.out.println("Exception raised is:"+ex);
}
}
//--------------------------------------------Ending with verifyLogin() method-------
//--------------------------------------------Starting with showResults() method------
void showResults(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
//Retrieving the Metadata from the resultset
int frecord = 0;
int nCols=rmeta.getColumnCount();
if(r.next())
{
// Retrieving the first field from the login table of the student.mdb database
String param1 = r.getString(1).trim();
String param2 = r.getString(2).trim();
if (param1.equals("administrator") && param2.equals("password"))
{
// Verifying for login "administrator" and password 'password'
// if found record, then set frecord = 1
frecord = 1;
//removing Login TAB
tbPane.removeTabAt(0);
// Displaying other panes
displayPanes();
}
/*String param2 = r.getString(2).trim();
if (param2.equals("password"))
{
// Verifying for login "administrator" and password 'password'
// if found record, then set frecord = 1
frecord = 1;
//removing Login TAB
tbPane.removeTabAt(0);
// Displaying other panes
displayPanes();
} */
else if(frecord==0)
{
//if frecord is zero, then display invalid login.
message_dialog = "Invalid Login name/password. Please enter again. ";
type_dialog = JOptionPane.INFORMATION_MESSAGE;
//Disaplaying the alert message
JOptionPane.showMessageDialog((Component) null, message_dialog, fail_dialog, type_dialog);
//Creating the tlogname and tlogpass textboxes empty
tlogname.setText(" ");
tlogpass.setText(" ");
}
}
else
{
message_dialog = "Invalid Login. Please Enter Again.";
type_dialog = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null, message_dialog, fail_dialog, type_dialog);
tlogname.setText(" ");
tlogpass.setText(" ");
}
}
//------------------------------------------------------------------- Ending with showResults() method -------
//=================================================================== Starting with addRecord() method====
void addRecord()
{
try
{
Class.forName(driver);
Connection con=DriverManager.getConnection(url);
Statement st = con.createStatement();
//Generating new register number
String query2 = "SELECT * FROM student_details";
ResultSet rs = st.executeQuery(query2);
int cnt = 0;
while (rs.next())
{
cnt++;
}
cnt = cnt+1;
String cs = (String)course.substring(0,3);
char ss = year.charAt(0);
reg_no = ""+cs+""+ss+"-"+cnt;
query3 = "INSERT INTO student_details (name,preaddress,permaaddress,fathername,dateofbirth,fatheroccupation,phone,gender,year,course,dmc,degree,charcerti,ncccerti) VALUES ('"+tsname.getText()+"','"+tspreaddress.getText()+"','"+tspermaaddress.getText()+"','"+tsfathername.getText()+"','"+tsdob.getText()+"','"+foccupation+"','"+tsphone.getText()+"','"+gender+"','"+year+"','"+course+"',"+dmcsel+","+degreesel+","+charcfsel+","+ncccfsel+")";
int rs_int=st.executeUpdate(query3);
con.close();
message_dialog = " "+tsname.getText().toUpperCase()+" Record is Added ";
type_dialog = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null, message_dialog, add_dialog, type_dialog);
// Adding the new record to the JTable
String [] data=new String[7];
data[0] = reg_no;
data[1] = tsname.getText();
data[2] = tspreaddress.getText();
data[3] = tspermaaddress.getText();
data[4] = tsphone.getText();
data[5] = tsfathername.getText();
data[6] = tsdob.getText();
defaulttablemodel.addRow(data);
cb_fatheroccupation.setSelectedIndex(0);
cb_course.setSelectedIndex(0);
cb_year.setSelectedIndex(0);
String snametemp = tsname.getText();
cb_sname.addItem(tsname.getText());
//Adding name to combobox in modify names TAB
//Setting other textboxes as blank to enable new record entry
tsname.setText(" ");
tspreaddress.setText(" ");
tsfathername.setText(" ");
// Uncheck the checkboxes, if they are checked while adding data
dmc.setSelected(false);
degree.setSelected(false);
ncc.setSelected(false);
charcerti.setSelected(false);
//clicking the radiobutton male , to make it default again
male.doClick();
}
catch(SQLException ex)
{
System.out.println("Unable to access the database");
}
catch(ClassNotFoundException ex)
{
System.out.println("Class not found");
}
catch(Exception ex)
{
System.out.println("Exception raised is:"+ex);
}
}
//------------------------------------------------------------------- Ending with addRecord() -------
//=============================================================== Starting with accessModifiedDatabase() ========
void accessModifiedDatabase(int nsel)
{
try
{
query4 = "SELECT * FROM student_details WHERE registration_no="+nsel+"";
Class.forName(driver);
Connection con=DriverManager.getConnection(url);
Statement st = con.createStatement();
boolean bresults = st.execute(query4);
if(bresults)
{
ResultSet result = st.getResultSet();
if(result!=null)
{
showModifiedResults(result);
}
con.close();
}
}
catch(SQLException ex)
{
System.out.println("Unable to access the database");
}
catch(ClassNotFoundException ex)
{
System.out.println("Class not found");
}
catch(Exception ex)
{
System.out.println("Exception raised is:"+ex);
}
}
//------------------------------------------------------------------- Ending with accessModifiedDatabase() -------
//=============================================================== Starting with showModifiedResults() ========
void showModifiedResults(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int frecord = 0;
int nCols=rmeta.getColumnCount();
String txt1="";
String txt2="";
String txt3="";
String txt4="";
String txt5="";
String txt6="";
String txt7="";
String txt8="";
String txt9="";
String txt10="";
String txt11="";
int int9=0;
int int10=0;
int int11=0;
int int12=0;
while(r.next())
{
reg_no=r.getString(1);
txt2+=r.getString(2);
txt3+=r.getString(3);
txt4+=r.getString(4);
txt5+=r.getString(5);
txt6+=r.getString(6);
txt7+=r.getString(7);
txt8+=r.getString(8);
txt9+=r.getString(9);
txt10=r.getString(10);
txt11=r.getString(11);
int9=r.getInt(12);
int10=r.getInt(13);
int11=r.getInt(14);
int12=r.getInt(15);
}
currentname = txt2;
tmname.setText(txt2);
tmpreaddress.setText(txt3);
tmpermaaddress.setText(txt4);
tmphone.setText(txt8);
tmfathername.setText(txt5);
tmdob.setText(txt6);
txt7 = txt7.trim();
if (txt7.equals("Govt. Employee "))
{
mcb_fatheroccupation.setSelectedIndex(0);
}
else if(txt7.equals("Self Employed"))
{
mcb_fatheroccupation.setSelectedIndex(1);
}
else
{
mcb_fatheroccupation.setSelectedIndex(2);
}
txt9 = txt9.trim();
if (txt9.equalsIgnoreCase("M"))
{
m_male.doClick();
}
else
{
m_female.doClick();
}
txt11 = txt11.trim();
if (txt11.equals("B.Sc.(Comp. Sc.)"))
{
mcb_course.setSelectedIndex(1);
}
else
{
mcb_course.setSelectedIndex(0);
}
txt10 = txt10.trim();
if (txt10.equals("I Year"))
{
mcb_year.setSelectedIndex(0);
}
else if(txt10.equals("II Year"))
{
mcb_year.setSelectedIndex(1);
}
else
{
mcb_year.setSelectedIndex(2);
}
if(int9==1)
{
mdmc.setSelected(true);
}
else
{
mdmc.setSelected(false);
}
if(int10==1)
{
mdegree.setSelected(true);
}
else
{
mdegree.setSelected(false);
}
if(int11==1)
{
mcharcerti.setSelected(true);
}
else
{
mcharcerti.setSelected(false);
}
if(int12==1)
{
mncc.setSelected(true);
}
else
{
mncc.setSelected(false);
}
}
//------------------------------------------------------------------- Ending with showModifiedResults() -------
//=============================================================== Starting with accessStudName() ========
void accessStudName() //Retrieving the student list in the combo box
{
try
{
query5 = "SELECT registration_no,Name FROM student_details ORDER BY registration_no";
Class.forName(driver);
Connection con=DriverManager.getConnection(url);
Statement st = con.createStatement();
boolean bresults = st.execute(query5);
if(bresults)
{
ResultSet result = st.getResultSet();
if(result!=null)
{
showStudName(result);
}
con.close();
}
}
catch(SQLException ex)
{
System.out.println("Unable to access the database");
}
catch(ClassNotFoundException ex)
{
System.out.println("Class not found");
}
catch(Exception ex)
{
System.out.println("Exception raised is:"+ex);
}
}
//------------------------------------------------------------------- Ending with accessStudName() -------
//=============================================================== Starting with showStudName() ========
void showStudName(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int nCols=rmeta.getColumnCount();
while(r.next())
{
cb_sname.addItem(r.getString(1)+":"+r.getString(2));
ctr_val++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -