首次接触jdbc编程,本来想写一个成绩查询系统,感觉自己的数据库基础还是很差,所以找啦一个比较简单的登录程序做
但是由于水平有限,自动登录和记住密码功能没有写出来,希望大家指点一下
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Landing extends JFrame implements ActionListener,ItemListener {
private String driver="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://127.0.0.1:3306/denglusql";
private String user="root";
private String pass="yeqing123";
//数据库的连接信息
private JFrame jf=new JFrame("登录系统");
private JLabel jl_user=new JLabel("账号");
private JLabel jl_pass=new JLabel("密码");
private JButton jb_zhuce=new JButton("注册");
private JButton jb_landing=new JButton("登录");
private JComboBox jtf_user=new JComboBox();
private JPasswordField jtf_pass=new JPasswordField();
private JPanel jp_certen=new JPanel(null);
private JPanel jp_main;
private JLabel jb_top=new JLabel();
private JLabel jb_base=new JLabel();
private JCheckBox jcb_mm=new JCheckBox("记住密码");
private JCheckBox jcb_dl=new JCheckBox("自动登录");
//一些定义以及实现这个登录功能的组件
public void LandingWindows(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jb_landing.addActionListener(this);
jb_zhuce.addActionListener(this);
setcerten();//设置主界面的主要布局
adduser();//记住登录的用户名
jp_main=new JPanel(new BorderLayout()){
@Override
public void paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D)g;
super.paintComponents(g);
Image image=Toolkit.getDefaultToolkit().getImage("F:\\JavaMedia\\landing2.jpg");
g2.drawImage(image,0,0,this.getWidth(),this.getHeight(),this);
}
};
//通过重写这个方法设置这个主见的背景图片,有很多实现方法,这是比较简单的方法吧,可以通过查阅api得到。
jp_main.add(jb_top,BorderLayout.NORTH);
jp_main.add(jb_base,BorderLayout.SOUTH);
jp_main.add(jp_certen,BorderLayout.CENTER);
jb_top.setPreferredSize(new Dimension(jf.getWidth(),30));
jb_base.setPreferredSize(new Dimension(jf.getWidth(),25));
//通过设置两个空白面板来调整整个布局的位置
jf.setContentPane(jp_main);
jf.setSize(390,250);
Dimension scrp=Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation((scrp.width-jf.getWidth())/2,(scrp.height-jf.getHeight())/2);
/*设置它的位置的常用方法吧,先得到它整个屏幕的宽高,然后用这种方法,不管在什么屏幕中都能显示在中间的
位置吧*/
jf.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
quit();
}
//退出ui的实现程序
});
jf.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
jf.setResizable(false);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb_zhuce){
zhuce();
}
if(e.getSource()==jb_landing){
denglu();
}
}
public void quit(){
int flag=0;
String mesg="你确定要退出此登录系统?";
flag=JOptionPane.showConfirmDialog(jf,mesg,"提示",JOptionPane.YES_NO_OPTION);
if(flag==JOptionPane.YES_OPTION){
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==jcb_mm){
getpass();
}
if(e.getSource()==jcb_dl){
denglu();
}
}
public void setcerten(){
jp_certen.setOpaque(false); //这个方法很重要,一定要设置false,才能显示出完整的背景图片
jl_user.setBounds(80,15,60,20);
jl_pass.setBounds(80,55,60,20);
jtf_user.setBounds(130,10,150,30);
jtf_pass.setBounds(130,50,150,30);
jcb_mm.setBounds(90, 90, 80, 20);
jcb_mm.addItemListener(this);
jcb_dl.setBounds(190,90,80,20);
jcb_mm.addItemListener(this);
jb_zhuce.setBounds(230,120,70,30);
jb_landing.setBounds(80,120,70,30);
jp_certen.add(jl_user);
jl_user.setForeground(Color.red);
jp_certen.add(jtf_user);
jtf_user.setEditable(true);
jp_certen.add(jl_pass);
jl_pass.setForeground(Color.red);
jp_certen.add(jcb_mm);
jp_certen.add(jcb_dl);
jp_certen.add(jtf_pass);
jp_certen.add(jb_landing);
jp_certen.add(jb_zhuce);
}
//主要的布局程序类
public void zhuce(){
String z_user=String.valueOf(jtf_user.getSelectedItem());
String z_pass=String.valueOf(jtf_pass.getPassword());
String sql;
if(z_user.trim().equals("")||z_pass.equals(""))
{
JOptionPane.showMessageDialog(jf,"账号或密码不能为空!!!","提醒",JOptionPane.INFORMATION_MESSAGE);
}else
{
Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
try { sql="select j_user from denglu where j_user=?";
conn=DriverManager.getConnection(url,user,pass);
psta=conn.prepareStatement(sql);
//prepareStatement比Statement强大啦很多
psta.setString(1,z_user);
rs=psta.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(jf, "该用户已注册,请重新输入用户名!!",
"提醒!",JOptionPane.INFORMATION_MESSAGE);
}else{
if(z_pass.length()>=6)
{sql="insert into denglu(j_user,j_pass) values(?,?)";
psta.setString(1,z_user);
psta.setString(2,z_pass);
conn=DriverManager.getConnection(url,user,pass);
psta=conn.prepareStatement(sql);
psta.executeUpdate();
JOptionPane.showMessageDialog(jf,"注册成功,请登录!!!");}
else{
JOptionPane.showMessageDialog(jf,"密码须大于六位!!!","提醒!",JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
try {
if(rs!=null){
rs.close();
rs=null;}
if(psta!=null){
psta.close();
psta=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//实现注册功能的类,通过使用insert into语句把数据加入到数据库中,实现注册功能
public void adduser(){
String a_user=String.valueOf(jtf_user.getSelectedItem());
String a_pass=String.valueOf(jtf_pass.getPassword());
String sql="select j_user from denglu;";
try {
Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement sta=conn.prepareStatement(sql);
ResultSet rs=sta.executeQuery();
rs.afterLast();
int i=0;
String s=null;
while(rs.previous()&&i<4){
i++;
s=rs.getString("j_user");
jtf_user.addItem(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//通过查找数据库来实现的记住前四个用户名的功能
public void getpass(){
String g_user=String.valueOf(jtf_user.getSelectedItem());
String g_pass=String.valueOf(jtf_pass.getPassword());
String sql="select j_user,j_pass from denglu where j_user=?";
Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
psta=conn.prepareStatement(sql);
psta.setString(1,g_user);
rs=psta.executeQuery();
String s=null;
while(rs.next()){
s=rs.getString("j_pass");
}
jtf_user.addItem(s);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(psta!=null){
psta.close();
psta=null;}
if(conn!=null){
conn.close();
conn=null;}
}catch (SQLException e){
e.printStackTrace();}
}
}
public void denglu(){
String d_user=String.valueOf(jtf_user.getSelectedItem());
String d_pass=String.valueOf(jtf_pass.getPassword());
String sql="select j_user,j_pass from denglu where j_user=?";
if(d_user.equals("")||d_pass.equals("")){
JOptionPane.showMessageDialog(jf,"登录密码或账户不能为空!!!!","警告,提醒!!!",JOptionPane.INFORMATION_MESSAGE);
}else{ Connection conn=null;
PreparedStatement psta=null;
ResultSet rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
psta=conn.prepareStatement(sql);
psta.setString(1,d_user);
rs=psta.executeQuery();
if(rs.next()){
if(d_pass.equals(rs.getString(2)))
{
JOptionPane.showMessageDialog(jf,"登录成功");
System.exit(0);
}else{
JOptionPane.showMessageDialog(jf,"密码输入错误!!!","提醒!!警告!!!",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(jf,"用户名不存在,请重新输入或注册一个账户!!!","提醒!!!",JOptionPane.INFORMATION_MESSAGE);
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(psta!=null){
psta.close();
psta=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
catch(SQLException e){
e.printStackTrace();
} }} }
public static void main(String[] args) {
new Landing().LandingWindows();
}
}