JDBC連線資料庫,完成註冊和登入
第一:建立user表
第二:完成登入和註冊介面
其中註冊介面程式碼
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ResigerUI extends JDialog implements ActionListener {
private UserService service = new UserServiceImpl();
private int width = 600;
private int height = 500;
private JLabel titlelb;
/** 使用者名稱標籤 */
private JLabel nameLb;
/** 密碼標籤 */
private JLabel passwordLb;
/** 性別標籤 */
private JLabel sexLb;
/** 郵箱標籤 */
private JLabel emailLb;
/** 聯絡電話標籤 */
private JLabel phoneLb;
/** 使用者名稱文字框 */
private JTextField nameTf;
/** 密碼文字框 */
private JPasswordField passwordTf;
/** 女性單選按鈕 */
private JRadioButton maleRbtn;
/** 男性單選按鈕 */
private JRadioButton femaleRbtn;
/** 聯絡電話文字框 */
private JTextField phoneTf;
/** 郵箱文字框 */
private JTextField emailTf;
/**確定按鈕*/
private JButton okBtn;
/**重置*/
private JButton resetBtn;
/** 構造方法 */
public ResigerUI() {
initComponents();
layoutComponents();
addListeners();
setSize(516, 483);// 設定大小
setModal(true);//設定為模態視窗
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);// 居螢幕中間顯示
}
/**
* 初始化元件
*/
private void initComponents() {
titlelb= new JLabel("注 冊");
nameLb = new JLabel("使用者名稱");
nameLb.setFont(new Font("宋體", Font.PLAIN, 15));
passwordLb = new JLabel("\u5BC6 \u7801");
passwordLb.setFont(new Font("宋體", Font.PLAIN, 15));
sexLb = new JLabel("\u6027 \u522B");
sexLb.setFont(new Font("宋體", Font.PLAIN, 15));
phoneLb = new JLabel("聯絡電話");
phoneLb.setFont(new Font("宋體", Font.PLAIN, 15));
emailLb = new JLabel("\u90AE \u7BB1");
emailLb.setFont(new Font("宋體", Font.PLAIN, 15));
nameTf=new JTextField();
passwordTf=new JPasswordField();
maleRbtn=new JRadioButton("男");
maleRbtn.setFont(new Font("宋體", Font.PLAIN, 15));
femaleRbtn=new JRadioButton("女");
femaleRbtn.setFont(new Font("宋體", Font.PLAIN, 15));
phoneTf=new JTextField();
emailTf=new JTextField();
okBtn = new JButton("確定");
okBtn.setFont(new Font("宋體", Font.PLAIN, 15));
resetBtn = new JButton("重置");
resetBtn.setFont(new Font("宋體", Font.PLAIN, 15));
}
/**
* 佈局元件
*/
private void layoutComponents() {
getContentPane().setLayout(null);// 去除預設的邊界佈局
titlelb.setBounds(182, 20, 80, 30);
titlelb.setFont(new Font("", Font.BOLD, 26));
nameLb.setBounds(100, 85, 80, 30);
nameTf.setBounds(200, 86, 250, 30);
passwordLb.setBounds(100, 143, 80, 30);
passwordTf.setBounds(200, 144, 250, 30);
sexLb.setBounds(100, 194, 80, 30);
maleRbtn.setBounds(200, 194, 50, 30);
femaleRbtn.setBounds(262, 194, 50, 30);
ButtonGroup btnGrounp = new ButtonGroup();
btnGrounp.add(maleRbtn);
btnGrounp.add(femaleRbtn);
maleRbtn.setSelected(true);
emailLb.setBounds(100,251,80,30);
emailTf.setBounds(200, 252, 250, 30);
phoneLb.setBounds(100,319,80,30);
phoneTf.setBounds(200, 320, 250, 30);
okBtn.setBounds(151, 386, 80, 30);
resetBtn.setBounds(289, 386, 80, 30);
getContentPane().add(titlelb);
getContentPane().add(nameLb);
getContentPane().add(nameTf);
getContentPane().add(passwordLb);
getContentPane().add(passwordTf);
getContentPane().add(sexLb);
getContentPane().add(maleRbtn);
getContentPane().add(femaleRbtn);
getContentPane().add(emailLb);
getContentPane().add(emailTf);
getContentPane().add(phoneLb);
getContentPane().add(phoneTf);
getContentPane().add(okBtn);
getContentPane().add(resetBtn);
}
/**
* 新增監聽器
*/
private void addListeners() {
okBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
/**
* 點選按鈕觸發的操作都寫在這裡
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == okBtn){//點選確定按鈕
resiger();//註冊
}else if(e.getSource()==resetBtn){//點選重置按鈕
reset();//重置
}
}
//註冊
private void resiger() {
String name = nameTf.getText();
String password = new String(passwordTf.getPassword());
String sex = "男";
if(femaleRbtn.isSelected()){
sex = "女";
}
String phonestr = phoneTf.getText();
Integer phone = new Integer(phonestr);
String email = emailTf.getText();
User user = new User();
user.setName(name);
user.setPassword(password);
user.setSex(sex);
user.setPhone(phone);
user.setEmail(email);
try {
service.resiger(user);
JOptionPane.showMessageDialog(this, "註冊成功");
dispose();
new LoginFrame().setVisible(true); //切換介面
} catch (ServiceException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
/**重置*/
private void reset() {
nameTf.setText("");
passwordTf.setText("");
maleRbtn.setSelected(true);
femaleRbtn.setSelected(false);
phoneTf.setText("");
emailTf.setText("");
nameTf.requestFocus();//獲得焦點
}
}
登入程式碼略寫,效果如下圖
檢視資料庫,已成功匯入資料
登入成功
關鍵程式碼(包括上一篇的JDBC工具類)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import day2.rowmapper.UserRowmapper;
import day2.util.JDBCUtils;
import day2.util.RowMapper;
import day3.dao.UserDao;
import day3.entity.User;
public class UserDaoImpl implements UserDao {
//根據使用者名稱查詢使用者
public User findByName(String name) {
Connection conn = null;
List<User> list = new ArrayList<User>();
try {
conn = JDBCUtils.getConnection();
String sql = "select * from user where name= ? ";
Object[] params = {name };
RowMapper rm = new UserRowmapper();
list = JDBCUtils.executeQuery(conn, sql, params, rm);
if (null != list && list.size() > 0) {
return list.get(0);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//使用者註冊
public User resiger(User user) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into user (name,password,sex,phone,email) "
+ "values (?,?,?,?,?)";
Object[] params = { user.getName(),user.getPassword(),user.getSex(),user.getPhone(),user.getEmail() };
JDBCUtils.executeUpdate(conn, sql, params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//使用者登入
public String login(String loginname, String loginpsd) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from user where name=? and password=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setObject(1, loginname);
pst.setObject(2, loginpsd);
ResultSet rs = pst.executeQuery();
if(rs.next()){
return null;
}
return "登入失敗,使用者名稱或密碼錯誤";
}catch (SQLException e) {
return "登入異常";
} finally {
JDBCUtils.close(conn);
}
}
}