Создание адресной книги в Java, и когда я пытаюсь отобразить экран, я получаю неизвестные ошибки символов, в классе AddressScreen - PullRequest
0 голосов
/ 07 ноября 2019

Итак, я создаю адресную книгу, которая загружает операторы SQL для добавления, обновления, удаления и удаления всех. При создании JFrame я продолжаю получать неизвестные символьные ошибки в классе AddressScreen, я включил все мои классы Java для справки, пытаясь найти то, что я пропустил или делаю неправильно. заранее благодарю за помощь

...

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class AddressScreen extends javax.swing.JDialog { 
 //create a JDialog Form 

 DefaultTableModel model; 
 AddressOptions options= new AddressOptions ();

//at the top of the class declare them 

//then create a method 
public void viewAddress () { 
    model.setRowCount(0); //empty table 
    ArrayList <Address> addresses= new ArrayList <Address> (); 
    addresses= options.viewAddress(); 

    if(addresses!=null) { 

        for(Address address: addresses) { 

            Object [] addAddress= {address.getId(), 
                address.getFname(), address.getLname(), 
                address.getEmail(), address.getPhone() 
            }; 

            model.addRow(addAddress); 

        } 
    } 
}

//in the constructor add them 
public AddressScreen(java.awt.Frame parent, boolean modal) { 
    super(parent, modal); 
    initComponents(); 

    model= (DefaultTableModel) addresstable.getModel(); 
    viewAddress(); 
} 

private void addresstableMouseClicked(java.awt.event.MouseEvent evt) {                                          

    int selectedRow= addresstable.getSelectedRow (); 
    fname.setText(model.getValueAt(selectedRow, 1).toString()); 
    lname.setText(model.getValueAt(selectedRow, 2).toString()); 
    email.setText(model.getValueAt(selectedRow, 3).toString()); 
    phone.setText(model.getValueAt(selectedRow, 4).toString()); 
}     

private void addaddressActionPerformed(java.awt.event.ActionEvent evt) {                                           
    message.setText(""); 
    String myfirstname= fname.getText(); 
    String mylastname= lname.getText(); 
    String myemail= email.getText(); 
    String myphone= phone.getText(); 

    options.addAddress(myfirstname, mylastname, myemail, myphone); 
    viewAddress(); 
    message.setText("the address is successfully added"); 
}      

private void updateworkerActionPerformed(java.awt.event.ActionEvent evt) {                                              
    message.setText(""); 
    String myfirstname= fname.getText(); 
    String mylastname= lname.getText(); 
    String myemail= email.getText(); 
    String myphone= phone.getText(); 

    int selectedRow= addresstable.getSelectedRow(); 
    if(selectedRow ==-1) { 

        //no row is selected 

        if(model.getRowCount()==0) { 

            //table is empty 
            message.setText("table is empty"); 
        } 

        else { 
            message.setText("please select an address to update"); 
        } 
    } 
    else { 
        int id= (int) model.getValueAt(selectedRow, 0); 
        options.updateAddress(id, myfirstname, mylastname, myemail, myphone); 
        viewAddress(); 
        message.setText("Address is successfully updated"); 
    } 
}                   

private void deleteAddressActionPerformed(java.awt.event.ActionEvent evt) {                                              

    message.setText(""); 
    int selectedRow= addresstable.getSelectedRow(); 

    if(selectedRow==-1) { 


        if(model.getRowCount()==0) { 

            message.setText("table is empty"); 
        } 

        else { 

            message.setText("select an address to delete"); 
        } 
    } 

    else { 

        int id= (int) model.getValueAt(selectedRow, 0); 
        options.deleteAddress(id); 
        viewAddress(); 
        message.setText("Address is deleted successfully"); 
    } 
}
}  

...

...

    import java.sql.*;
import java.util.*;
import java.util.logging.*;

public class AddressOptions{

private Connection connection= null;
private Statement statement= null;
private PreparedStatement preparedstatement= null;

//anonymous class constructor
public AddressOptions(){

    try{
        connection= DriverManager.getConnection(Database.DATABASE_URL,Database.USERNAME,
                Database.PASSWORD);
        System.out.println("Loggin Successful");
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }
}// end anonymous class 

public boolean login (String username, String password){
    try{
        String query="select * from ADMIN where USERNAME =? AND PASSWORD =?";
        preparedstatement= connection.prepareStatement(query);
        preparedstatement.setString(1, username);
        preparedstatement.setString(2, password);

        // select returns resultSet
        ResultSet rs= preparedstatement.executeQuery();
        return rs.next();// if true then username and password are in query
    }
    catch(SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }
    return false;
}// end login

public ArrayList <Address> viewAddress(){
    try{
        ArrayList <Address> addresses= new ArrayList <Address> ();
        String query="select * from ADDRESSES";
        statement= connection.createStatement();
        ResultSet rs= statement.executeQuery(query);
        while(rs.next()){
            int id=rs.getInt("ADDRESSID");
            String fname= rs.getString("FIRSTNAME");
            String lname= rs.getString("LASTNAME");
            String email= rs.getString("EMAIL");
            String phone= rs.getString("PHONENUMBER");
            addresses.add(new Address(id,fname,lname,email,phone));
        }
        return addresses;
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
        return null;// when the table is empty
    }
}// end ArrayList <Address>

public void addAddress(String fname, String lname, String email, String phone){
    try{
        String query="insert into ADDRESSES (FIRSTNAME, LASTNAME, EMAIL, PHINENUMBER) values(?,?,?,?)";
        preparedstatement= connection.prepareStatement(query);
        preparedstatement.setString(1,fname);
        preparedstatement.setString(2,lname);
        preparedstatement.setString(3,email);
        preparedstatement.setString(4,phone);
        preparedstatement.executeUpdate();
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }

}// end addAddress

public void deleteAddress (int id){
    try{
        String query="delet from ADDRESSES where ADDRESSID =?";
        preparedstatement= connection.prepareStatement(query);
        preparedstatement.setInt(1,id);
        preparedstatement.executeUpdate();
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }
}// end deleteAddress

public void updateAddress(int id, String fname, String lname, String email, String phone){
    try{
        String query="update ADDRESSES set FIRSTNAME=?, LASTNAME=?, EMAIL=?, PHONENUMBER=? where ADDRESSID=?";
        preparedstatement= connection.prepareStatement(query);
        preparedstatement.setString(1, fname);
        preparedstatement.setString(2, lname);
        preparedstatement.setString(3, email);
        preparedstatement.setString(4, phone);
        preparedstatement.setInt(5, id);
        preparedstatement.executeUpdate();
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }
}// end updateAddress

public void deleteAll(){
    try{
        String query="delete from ADDRESSES";
        preparedstatement= connection.prepareStatement(query);
    }
    catch (SQLException ex){
        Logger.getLogger(AddressOptions.class.getName()).log(Level.SEVERE,null,ex);
    }
}// end deleteAll
}// end AddressOptions

...

...

public class Address {
private int id;
private String fname;
private String lname;
private String email;
private String phone;

//annonymous class constructor
public Address(int id, String fname, String lname, String email, String phone){
    this.id=id;
    this.fname=fname;
    this.lname=lname;
    this.email=email;
    this.phone=phone;
}// end annonymous constuctor

public int getId(){
    return id;
}
public void setID(){
this.id=id;
}
public String getFname(){
return fname;
}
public void setFname(){
this.fname=fname;
}
public String getLname(){
return lname;
}
public void setLname(){
this.lname=lname;
}
public String getEmail(){
return email;
}
public void setEmail(){
this.email=email;
}
public String getPhone(){
return phone;
}
public void setPhone(){
this.phone=phone;
}
}

... ...

public class Database { 

public static final String DATABASE_URL="jdbc:dery://localhost:1527/addressbook";
public static final String USERNAME="uta"; 
public static final String PASSWORD="12345"; 

} 

...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...