?? adrian-example.java
字號:
Public viod deleteRecord()throws SQLException{
Rsperson.deleteRow();
MoceFirst();
} // deleteRecord
Public viod displayMessage(String message,String title, int messageType) {
Joptionpane.showMessageDialog(this,message,title,messageType);
} // displayMessage
Public viod fillText(String.valueOf(rsperson.getInt("id")));
TxtFirstname.setText(rsPerson.getString("firstname"));
TxtFirstname.setText(rsPerson.getString("lastname"));
TxtPhone.setText(rsPerson.getSting("phone"));
} // fillTextFields
public viod finalize() {
try{
close();
} catch (SQLException e) {}
} // finalize
Public viod findRecord() throws SQLException{
Int id = 0;
String ID = null;
Try {
ID = JoptionPane.ShowInputDialog("Enter the person ID:");
Id = Integer.parseInt (ID);
RsPerson.beforeFirst();
While (rsPerson.next()){
If (rsperson.getInt("id")= =id ){
// current record is a match
break;
} // if
} // while
if (rsperson.isAfterLast()){
displayMessage("No record found","Find Record Message "
,Joptionpane.INFORMATION_MESSAGE);
rsperson.first();
} // if
fillTextFields();
} catch(Number FormatExeption nfe){
displayMessage("Enter a number for ID","INOUT VALTION", Joption.INFORMATION_MESSAGE);
} // catch
} // findRecord
// Implement the FocusListener methods
Public viod focusGained(FocusEvent e){}
Public viod focusLost(FocusEvent e ){
If (e .getSource() == txtID){
String ID = txtID.getText( ).trim( );
If (ID.length()>0 ){ //only validate if there is text entered
If (!validate( ID, " 打不出來d+",10)){
Disp LayMessage("Enter digits only for the ID number ",
"Validation Input Message",JoptionPane.INFORMATION_MESSAGE);
txtID. SelectAll( );
txtID. RequestFocus( );
}
}
} else if (e.getSource( )== txtphone){
string phone = txtphone . getText( ).trim( ) ;
if (phone .length() > 0) { // only validate if there is text entered
if (!validate(phone, " d+", 8 )){
displayMessage ("Enter digits only for phone number","Validation Input Message", Joptionpane.INFORMATION_MESSAGE);
txtphone.selectAll( );
txtphone. RequestFocus();
}
}
} else { // name fields left
JtextFied tf = (JTextFied) e.getSource();
String text = tf.getText()trim();
if(text.length()>0){
if(!validate(text,"\\p{Alpha}+",20)){ //Firstname and
Lastname fields
displayMessage("Enter a valid name:\nMaximum 20 letters",
"INPUT VALIDATION MESSAGE",
JOptionPane.INFORMATION_MESSAGE);
tf.selectAll(); //Selects and highlaghts text
tf.requestFocus();
}
}
}
} // focusLost
/**
*getErrorMessages iterates through the chain
*of SQLExceptions thrown by code
*@params The SQLException object at the start of the chain
*/
public String getErrorMessages(SQLException e){
stringBuffer sb = new StringBuffer();
while(e !=null){
sb.append(e.getMessage() + "\n");
sb.append(e.getSQLState() + "\n");
sb.append(e.getErrorCode() +"\n");
e =e.getNextException();
}
return sb.tostring();
} // getErrorMessages
/**
* InsertRecord inserts a new record into the
* ResultSet programmatically.
* @throws SQLException
*/
public viod insertRecord() throws SQLException {
rsPerson.moveToInsertRow();
rsPerson.updateInt("id",Integer.parseInt(txtID.getText()));
rsPerson.updateString("firstname",txtFirstname.getText()));
rsPerson.updateString("lastname",txtLastname.getText());
rsPerson.updateString("phone", txtPhone.getText());
rsPerson.insertRow();
rsPerson.moveToCurrentRow();
} // insertRecord
/**
*loadDriver attempts to load a JDBC compliant driver
*class supplied by the Vendor.
*First attempt is the MYSQL driver
*second attempt is the ODBC-JDBC bridge
*@return true for success or lalse for failure
*/
public boolean liadDriver(){
boolean suceess = true;
try{
// try to load the MYSQL driver first
Class.forName(driverMYSQL);
selectedDriver = driverMYSQL;
return success; // If you're here - mySQL driver found
}catch (ClassNotFoundException e){
// do nothing here.Flow continues with second attempt.
} // try1
try{
// try to load the ODBC bridge driver for MSAccess
Class.forName(driverMSAccess);
selectedDriver = driverMSAsccess;
}catch (ClassNotFoundException e){
//All is failed here
displayMessage("No Driver found - shutting down..","Database
Message",
success = false;
} // try2
return success;
}//loadDriver
/**
*keyPressed transfers focus to the next field
*when [Enter] is pressed.
*@param The KeyEvent object generated
*/
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.vk_ENTER){
((JTextField)e.getSource()).transferFocus();
}
}// keyPressed
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void unlockFields(boolean state){
txtID.setEditable(state);
txtID.setBackground(Color.WHITE);
txtFirstname.setEditable(state);
txtFirstname.setBackgroud(Color.WHITE);
txtLastname.setEditable(state);
txtLastname.setBackground(Color.WHITE);
txtPhone.setEditable(state);
txtPhone.setBackground(Color.WHITE);
} // unlockFields
public void moveFirst() throws SQLException{
if(rsPerson.first()){
fillTextFields());
}
}// moveFirst
poblic void moveLast()throws SQLException{
if(rsPerson.last()){
fillTextFields());
}
}// moveLast
public void moveNext()throws SQLException{
if(rsPerson.next()){
fillTextFields();
public coid movePrevious() throws SQLException{
if(rsPerson.previous()){
fillTextFields();
}
public void setAddMode(){
addMode = true ;
setViewButtons(false);
unlockFields(true);
clearFields();
txtID.requestFocus();
} // setAddMode
public void setEditMode (){
editMode = true;
setViewButtons(false);
unlockFields(true);
txtID.setEditable (false);
txtFirstname.requestFocus();
} // setEditMode
public void setViewButtons(boolean state){
btnAdd.setEnabled(state);
btnCancel.setEnabled(!state);
btnDelete.setEnabled(state);
btnEdit.setEnabled(state);
btnSave.setEnabled(!state);
btnFirst.setEnabled(state);
btnNext.setEnabled(state");
btnPrevious.setEnabled(state);
btnLast.setEnabled(state);
} // setVIewButtons
public void setViewMode(){
addMode = false;
editMode = false;
setViewButtons(true);
unlockFields(false);
btnFirst.requestFocus();
} // setViewMode
public void updateRecord()throws SQLException{
// The cursor is at current record
rsPerson.updateString("firstname",txtFirstname.getText());
rsPerson.updateString("lastname",txtLasrname.getText());
rsPerson.updateString("phone",txtPhone.getText());
rsPerson.updateRow();
} // updateRecord
public boolean validate(String text.String mask, int maxLength){
if(text.length()>maxLength)
return false;
Pattern p = Pattern.compile(mask);
Matcher m = p.matcher(text);
boolean result = m.matches();
return result;
} // validate
public static void main (String[] args){
PersonRecordDB prdb = new PersonRecordDB();
} //main
}// PersonRecordPanelSQL
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -