我目前正在使用JDBC存储客户输入数据库的电话号码,然后将电话号码显示在JTable
中。我可以在不使用TableCellRenderer
的情况下让它工作,但我希望数字在表中显示为(区域)前缀数字格式。下面是我用来尝试实现这一点的代码:
//core package
public class Patient {
private int id;
private String firstName;
private String middleName;
private String lastName;
private String phneNum;
private String email;
public Patient(String firstName, String middleName, String lastName, String phneNum, String email) {
this(0, firstName, middleName, lastName, phneNum, email);
}
public Patient(int id, String firstName, String middleName, String lastName, String phneNum, String email) {
super();
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.phneNum = phneNum;
this.email = email;
}
public int getId() {
return id;
}
public void setId() {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName() {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName() {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName() {
this.lastName = lastName;
}
public String getPhneNum() {
return phneNum;
}
public void setPhneNum() {
this.phneNum = phneNum;
}
public String getEmail() {
return email;
}
public void setEmail() {
this.email = email;
}
@Override
public String toString() {
return String.format("Patient [id=%s, firstName=%s, middleName=%s, lastName=%s, phneNum=%s, email=%s",
id, firstName, middleName, lastName, phneNum, email);
}
}
public class PhoneNumber {
public String areaCode;
public String prefix;
public String num;
private PhoneNumber() {}
public PhoneNumber(String areaCode, String prefix, String num) {
this.areaCode = areaCode;
this.prefix = prefix;
this.num = num;
}
@Override
public String toString() {
return "areaCode=" + areaCode + "; prefix=" + prefix + "; num=" + num;
}
}
//DAO Package
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.patientsessiontracker.core.*;
public class PatientDAO {
private Connection dbConn;
public PatientDAO() throws Exception {
// get db properties
Properties config = new Properties();
config.load(new FileInputStream("Patient Session Tracker Config/patientSessionTracker.properties"));
String user = config.getProperty("user");
String password = config.getProperty("password");
String dburl = config.getProperty("dburl");
// connect to database
dbConn = DriverManager.getConnection(dburl, user, password);
System.out.println("DB connection successful to: " + dburl);
}
public void addPatient(Patient thePatient) throws Exception {
PreparedStatement dbStmt = null;
try {
//prepare statement
dbStmt = dbConn.prepareStatement("insert into patients " +
"(first_name, middle_name, last_name, phone_number, email) " +
"values (?, ?, ?, ?, ?)");
// set params
dbStmt.setString(1, thePatient.getFirstName());
dbStmt.setString(2, thePatient.getMiddleName());
dbStmt.setString(3, thePatient.getLastName());
dbStmt.setObject(4, thePatient.getPhneNum());
dbStmt.setString(5, thePatient.getEmail());
// execute the query
dbStmt.executeUpdate();
} finally {
close(dbStmt);
}
}
public List<Patient> getAllPatients() throws Exception {
List<Patient> list = new ArrayList<>();
Statement dbStmt = null;
ResultSet dbRs = null;
try {
dbStmt = dbConn.createStatement();
dbRs = dbStmt.executeQuery("select * from patients");
while (dbRs.next()) {
Patient tempPatient = convertRowToPatient(dbRs);
list.add(tempPatient);
}
return list;
} finally {
close(dbStmt, dbRs);
}
}
public List<Patient> searchPatients(String firstName, String lastName) throws Exception {
List<Patient> list = new ArrayList<>();
PreparedStatement dbStmt = null;
ResultSet dbRs = null;
try {
firstName += "%";
lastName += "%";
dbStmt = dbConn.prepareStatement("select * from patients where first_name like ? and last_name like ?");
dbStmt.setString(1, firstName);
dbStmt.setString(2, lastName);
dbRs = dbStmt.executeQuery();
while (dbRs.next()) {
Patient tempPatient = convertRowToPatient(dbRs);
list.add(tempPatient);
}
return list;
} finally {
close(dbStmt, dbRs);
}
}
private Patient convertRowToPatient(ResultSet dbRs) throws SQLException {
int id = dbRs.getInt("id");
String firstName = dbRs.getString("first_name");
String middleName = dbRs.getString("middle_name");
String lastName = dbRs.getString("last_name");
String phneNum = dbRs.getString("phone_number");
String email = dbRs.getString("email");
Patient tempPatient = new Patient(firstName, middleName, lastName, phneNum, email);
return tempPatient;
}
private static void close(Connection dbConn, Statement dbStmt, ResultSet dbRs) throws SQLException {
if (dbRs != null)
dbRs.close();
if (dbStmt != null)
if (dbConn != null)
dbConn.close();
}
private static void close(Statement dbStmt, ResultSet dbRs) throws SQLException {
close(null, dbStmt, dbRs);
}
private void close(Statement dbStmt) throws SQLException {
close(null, dbStmt, null);
}
public static void main(String[] args) throws Exception {
PatientDAO dao = new PatientDAO();
System.out.println(dao.getAllPatients());
}
}
//UI Package
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import java.util.List;
import com.patientsessiontracker.core.*;
import com.patientsessiontracker.dao.*;
public class PatientSessionTrackerApp extends JFrame {
private JPanel contentPane;
private JTextField txtFieldSrchFName;
private JButton btnSearch;
private JScrollPane scrollPane;
private static JTable table;
private static PatientDAO patientDAO;
private JPanel panelBtn;
private JButton btnAddPatient;
private JTextField txtFieldSrchLName;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PatientSessionTrackerApp frame = new PatientSessionTrackerApp();
frame.setVisible(true);
PatientSessionTrackerApp.refreshPatientsView();
table.getColumnModel().getColumn(3).setCellRenderer(new PatientSessionTrackerCellRenderer());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PatientSessionTrackerApp() {
// create the DAO
try {
patientDAO = new PatientDAO();
} catch (Exception exc) {
JOptionPane.showMessageDialog(this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
}
setTitle("Patient Session Tracker");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panelSrch = new JPanel();
panelSrch.setBounds(5, 5, 579, 33);
contentPane.add(panelSrch);
panelSrch.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JLabel lblFirstName = new JLabel("First Name");
panelSrch.add(lblFirstName);
txtFieldSrchFName = new JTextField();
panelSrch.add(txtFieldSrchFName);
txtFieldSrchFName.setColumns(13);
btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get last & first name from the text field
// Call DAO and get patients for the last & first name
// If last & first name is empty, then get all patients
try {
String firstName = txtFieldSrchFName.getText();
String lastName = txtFieldSrchLName.getText();
List<Patient> patients = null;
if (((firstName != null) && (firstName.trim().length() > 0)) ||
((lastName != null) && (lastName.trim().length() > 0)))
patients = patientDAO.searchPatients(firstName, lastName);
else
patients = patientDAO.getAllPatients();
// create the model and update the "table"
PatientTableModel model = new PatientTableModel(patients);
table.setModel(model);
table.getColumnModel().getColumn(3).setCellRenderer(new PatientSessionTrackerCellRenderer());
} catch (Exception exc) {
JOptionPane.showMessageDialog(PatientSessionTrackerApp.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
JLabel lblLastName = new JLabel("Last Name");
panelSrch.add(lblLastName);
txtFieldSrchLName = new JTextField();
panelSrch.add(txtFieldSrchLName);
txtFieldSrchLName.setColumns(13);
panelSrch.add(btnSearch);
scrollPane = new JScrollPane();
scrollPane.setBounds(5, 38, 579, 186);
contentPane.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
panelBtn = new JPanel();
panelBtn.setBounds(5, 224, 579, 38);
contentPane.add(panelBtn);
btnAddPatient = new JButton("Add Patient");
btnAddPatient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// create dialog
AddPatientDialog addPatient = new AddPatientDialog(PatientSessionTrackerApp.this, patientDAO);
// show dialog
addPatient.setModal(true);
addPatient.setVisible(true);
}
});
panelBtn.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panelBtn.add(btnAddPatient);
}
public static void refreshPatientsView() {
try {
List<Patient> patients = patientDAO.getAllPatients();
// create the model and update the "table"
PatientTableModel model = new PatientTableModel(patients);
table.setModel(model);
} catch (Exception exc) {
JOptionPane.showMessageDialog(null, "Error: " + exc, "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import com.patientsessiontracker.core.*;
import com.patientsessiontracker.dao.*;
public class AddPatientDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField txtFieldFName;
private JTextField txtFieldMName;
private JTextField txtFieldLName;
private JTextField txtFieldAreaCode;
private JTextField txtFieldPhneNum;
private JTextField txtFieldPhnePrefix;
private JTextField txtFieldEmail;
private PatientDAO patientDAO;
private PatientSessionTrackerApp patientSessionTrackerApp;
public AddPatientDialog(PatientSessionTrackerApp thePatientSessionTracker, PatientDAO thePatientDAO) {
this();
patientDAO = thePatientDAO;
patientSessionTrackerApp = thePatientSessionTracker;
}
public final class LengthRestrictedDocument extends PlainDocument {
private final int limit;
public LengthRestrictedDocument(int limit) {
this.limit = limit;
}
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offs, str, a);
}
}
}
/**
* Create the dialog.
*/
public AddPatientDialog() {
setSize(450, 300);
setResizable(false);
setTitle("Add New Patient");
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblFName = new JLabel("<html>First Name<font color='red'>*</font></html>");
lblFName.setBounds(10, 11, 104, 14);
contentPanel.add(lblFName);
txtFieldFName = new JTextField();
txtFieldFName.setBounds(124, 8, 310, 20);
contentPanel.add(txtFieldFName);
txtFieldFName.setColumns(10);
JLabel lblMName = new JLabel("Middle Name");
lblMName.setBounds(10, 36, 104, 14);
contentPanel.add(lblMName);
txtFieldMName = new JTextField();
txtFieldMName.setBounds(124, 33, 310, 20);
contentPanel.add(txtFieldMName);
txtFieldMName.setColumns(10);
JLabel lblLName = new JLabel("<html>Last Name<font color='red'>*</font></html>");
lblLName.setBounds(10, 61, 104, 14);
contentPanel.add(lblLName);
txtFieldLName = new JTextField();
txtFieldLName.setBounds(124, 58, 310, 20);
contentPanel.add(txtFieldLName);
txtFieldLName.setColumns(10);
JLabel lblPhneNum = new JLabel("Phone Number");
lblPhneNum.setBounds(10, 86, 104, 14);
contentPanel.add(lblPhneNum);
JLabel labelLP = new JLabel("(");
labelLP.setBounds(124, 86, 12, 14);
contentPanel.add(labelLP);
txtFieldAreaCode = new JTextField();
txtFieldAreaCode.setBounds(134, 83, 28, 20);
contentPanel.add(txtFieldAreaCode);
txtFieldAreaCode.setColumns(10);
txtFieldAreaCode.setDocument(new LengthRestrictedDocument(3));
}
JLabel labelRP = new JLabel(")");
labelRP.setBounds(166, 86, 12, 14);
contentPanel.add(labelRP);
txtFieldPhneNum = new JTextField();
txtFieldPhneNum.setBounds(210, 83, 35, 20);
contentPanel.add(txtFieldPhneNum);
txtFieldPhneNum.setColumns(10);
txtFieldPhneNum.setDocument(new LengthRestrictedDocument(4));
txtFieldPhnePrefix = new JTextField();
txtFieldPhnePrefix.setColumns(10);
txtFieldPhnePrefix.setBounds(172, 83, 28, 20);
contentPanel.add(txtFieldPhnePrefix);
txtFieldPhnePrefix.setDocument(new LengthRestrictedDocument(3));
JLabel lblDash = new JLabel("-");
lblDash.setBounds(202, 83, 12, 14);
contentPanel.add(lblDash);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Add Patient");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
savePatient();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
JTextArea txtAddPatientWarning = new JTextArea();
txtAddPatientWarning.setText("WARNING!!! Once a patient is created it is added to the database and cannot be removed from the database via the Patient Session Tracker. If a patient is added in error, please contact Sudo Technologies at (701) 388-3752.");
txtAddPatientWarning.setBounds(10, 132, 424, 60);
txtAddPatientWarning.setEditable(false);
txtAddPatientWarning.setBackground(Color.YELLOW);
txtAddPatientWarning.setLineWrap(true);
contentPanel.add(txtAddPatientWarning);
JLabel lblEmail = new JLabel("Email");
lblEmail.setBounds(10, 110, 46, 14);
contentPanel.add(lblEmail);
txtFieldEmail = new JTextField();
txtFieldEmail.setColumns(10);
txtFieldEmail.setBounds(124, 107, 310, 20);
contentPanel.add(txtFieldEmail);
JLabel lblRqrdField = new JLabel("<html><font color='red'>*</font> Denotes Required Field</html>");
lblRqrdField.setBounds(10, 213, 235, 26);
contentPanel.add(lblRqrdField);
}
protected void savePatient() {
// get the patient info from the gui
String firstName = txtFieldFName.getText();
String middleName = txtFieldMName.getText();
String lastName = txtFieldLName.getText();
//PhoneNumber phneNum = new PhoneNumber(txtFieldAreaCode.getText(), txtFieldPhnePrefix.getText(), txtFieldPhneNum.getText());
String areaCode = txtFieldAreaCode.getText();
String prefix = txtFieldPhnePrefix.getText();
String num = txtFieldPhneNum.getText();
String phneNum = areaCode + prefix + num;
String email = txtFieldEmail.getText();
Patient tempPatient = new Patient(firstName, middleName, lastName, phneNum, email);
if ((firstName.trim().length() != 0) && (lastName.trim().length() != 0)) {
try{
// save to the database
patientDAO.addPatient(tempPatient);
// close dialog
setVisible(false);
dispose();
// refresh gui list
patientSessionTrackerApp.refreshPatientsView();
// show success message
JOptionPane.showMessageDialog(patientSessionTrackerApp, "Patient added to the database.",
"Patient Added",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception exc) {
JOptionPane.showMessageDialog(patientSessionTrackerApp,
"Error saving patient: " +
exc.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
} else
JOptionPane.showMessageDialog(patientSessionTrackerApp, "<html>Please fill in all required fields as denoted by the <font color='red'>*</font>.</html>");
}
}
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import com.patientsessiontracker.core.*;
public class PatientSessionTrackerCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public String areaCode;
public String prefix;
public String num;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
PhoneNumber phneNum = (PhoneNumber) value;
String text = "(" + phneNum.areaCode + ") " + phneNum.prefix + "-" + phneNum.num;
setHorizontalAlignment(SwingConstants.CENTER);
setText(text);
return this;
}
}
import javax.swing.table.AbstractTableModel;
import java.util.List;
import com.patientsessiontracker.core.*;
class PatientTableModel extends AbstractTableModel {
private static final int FIRST_NAME_COL = 0;
private static final int MIDDLE_NAME_COL = 1;
private static final int LAST_NAME_COL = 2;
private static final int PHONE_NUMBER_COL = 3;
private static final int EMAIL_COL = 4;
private String[] columnNames = {"First Name", "Middle Name", "Last Name", "Phone Number", "Email"};
private List<Patient> patients;
public PatientTableModel(List<Patient> thePatients) {
patients = thePatients;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return patients.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
Patient tempPatient = patients.get(row);
switch (col) {
case FIRST_NAME_COL:
return tempPatient.getFirstName();
case MIDDLE_NAME_COL:
return tempPatient.getMiddleName();
case LAST_NAME_COL:
return tempPatient.getLastName();
case PHONE_NUMBER_COL:
return tempPatient.getPhneNum();
case EMAIL_COL:
return tempPatient.getEmail();
default:
return tempPatient.getFirstName();
}
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
我尝试将其作为对象传递到DAO中,但还没有找到解决方案。运行时,我得到了错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to com.patientsessiontracker.core.PhoneNumber
at com.patientsessiontracker.ui.PatientSessionTrackerCellRenderer.getTableCellRendererComponent(PatientSessionTrackerCellRenderer.java:22)
例外情况是,不能将字符串
强制转换为电话号码
对象。当你这么做的时候,这一切都在发生
PhoneNumber phneNum = (PhoneNumber) value;
您需要一种将字符串解析为电话号码对象的方法。如果你不强制输入特定格式的电话号码,这可能会很棘手。如果是,那么可以在PhoneNumber
中创建一个方法,将字符串解析为电话号码。比如:
public PhoneNumber parsePhoneNumber(String phoneStr){
// Insert logic to extract your area code, prefix and number
return new PhoneNumber(aC, pref, num);
}
然而,我看到您将电话号码存储在数据库中,只是作为3个号码的串联......这意味着解析字符串几乎是不可能的。
你应该考虑在整个代码中使用PhoneNumber
对象。例如,在您的患者
中,而不是字符串
public class Patient {
private int id;
private String firstName;
private String middleName;
private String lastName;
private PhoneNumber phneNum;
private String email;
public Patient(String firstName, String middleName, String lastName, PhoneNumber phneNum, String email) {
this(0, firstName, middleName, lastName, phneNum, email);
}
public Patient(int id, String firstName, String middleName, String lastName, PhoneNumber phneNum, String email) {
super();
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.phneNum = phneNum;
this.email = email;
}
//....
那么,唯一需要担心转换的地方就是在读写数据库时。
在我的iOS应用程序中,我必须转换一个电话号码(在联系人),并将其转换为国际格式(自动发送短信与extern库)。我看到了libPhoneNumber,但问题是我们必须输入国家代码,应用程序必须在所有(几乎)国家工作,所以我不知道用户的国家是什么。 以下是图书馆的工作原理:
问题内容: 我有一个数据库,其中包含数以百万计的免费格式的电话号码。即,UI不强制执行任何约束,并且用户正在键入他们想要的任何内容。 我正在寻找的是一种Java API,可以尽力将它们转换为一致的格式。理想情况下,API将采用自由文本值和国家/地区代码,并产生有效的国际电话号码或引发异常。 例如,系统中的电话号码可能类似于以下任何一项: 给定美国所在的国家/地区,API将为所有这些产生值“ +1(
问题内容: 一旦用户开始将电话号码键入此格式类型,并且我可以正常工作,我就在格式化文本文件中的文本,但是一旦我从服务器上获得了电话号码,我就这样得到了,所以请您告诉我如何编辑它我从服务器获得相同格式的文件吗? 输入开始后的代码: 问题答案: 用String中的字符进行操作不是很简单。您需要遵循以下条件: 斯威夫特2.1 雨燕2.0 代码将打印
问题内容: 我想知道如何使用angularjs指令在输入字段中自动设置数字格式?在输入字段中输入6042919283时,我希望它显示为604-291-9283。 谢谢 问题答案: 您可以使用UI Utils遮罩。 它允许您定义允许的输入格式,并将为您处理验证以及格式设置
问题内容: 我正在使用 SMS应用程序 ,需要能够将发件人的电话号码从 +11234567890转换 为 123-456-7890, 以便可以将其与 MySQL数据库中的 记录进行比较。 数字以后一种格式存储,可在网站上的其他地方使用,我宁愿不更改该格式,因为它将需要修改大量代码。 我将如何使用PHP? 谢谢! 问题答案:
在我的应用程序中,我想创建自己的电话号码格式,但仍然找不到实现这一点的方法。 我阅读了有关电话号码库等的信息,但仍然找不到这样做的方法,因为所有库都可以正常使用、、和其他格式。我想支持,但这些库不能正常工作。 唯一的方法是我必须创建自己的格式。 例如,如果我打字 它必须像这里一样格式化 我找到了IOS操作系统的libs(SHSPhoneTextField),但在Android操作系统中我没有找到。