package Login1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.mysql.jdbc.ResultSetMetaData;
public class Login1 {
JFrame jFrame;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login1().init();
}
public void init()
{
jFrame=new JFrame();
jFrame.setBounds(100, 100, 1000, 1000);
jFrame.setLayout(null);
JTextField userNameT=new JTextField("输入用户名");
userNameT.setBounds(100, 100, 150,50);
jFrame.add(userNameT);
JTextField passwordT=new JTextField("输入密码");
passwordT.setBounds(300, 100,150, 50);
jFrame.add(passwordT);
JButton jButton=new JButton("登录");
jButton.setBounds(150, 200, 100, 100);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//1.驱动实例化
Operation operation=new Operation();
String userName=userNameT.getText().trim();
String password=passwordT.getText().trim();
String sql="select * from login where username='"+userName+"'";
ResultSet resultSet=operation.query(sql);
try {
resultSet.last();
int row=resultSet.getRow();
if(row==0)
System.out.println("用户名不存在");
else {
if(resultSet.getString("password").equals(password))
System.out.println("登录成功");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
operation.close();
}
});
jFrame.add(jButton);
JButton jButton2=new JButton("注册");
jButton2.setBounds(250, 200, 100, 100);
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Operation operation=new Operation();
String userName=userNameT.getText().trim();
String password=passwordT.getText().trim();
String sqlinfo="insert into userinfo values ('"+userName+"',null,null,null,null)";//注册完成后插入数据库
String sqllogin="select * from login where username='"+userName+"'";
ResultSet resultSet=operation.query(sqllogin);
try {
resultSet.last();
int row=resultSet.getRow();
if(row>0)
System.out.println("用户名已存在");
else {
sqllogin="insert into login values('"+userName+"','"+password+"')";
if(operation.updata(sqllogin)>0)
{
operation.updata(sqlinfo);
System.out.println("注册成功");
}
else {
System.out.println("注册失败");
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
jFrame.add(jButton2);
JLabel jLabel =new JLabel("用户个人信息");
jLabel.setBounds(150, 300, 100, 50);
jFrame.add(jLabel);
JTextField nameT=new JTextField("输入姓名");
JTextField phoneT=new JTextField("输入手机号");
JTextField idnumT=new JTextField("输入身份证号");
JTextField birthdayT=new JTextField("出生日期:1990-10-10");
nameT.setBounds(150, 350, 100, 50);
phoneT.setBounds(150, 400, 100, 50);
idnumT.setBounds(150, 450, 100, 50);
birthdayT.setBounds(150, 500, 100, 50);
jFrame.add(nameT);
jFrame.add(phoneT);
jFrame.add(idnumT);
jFrame.add(birthdayT);
jFrame.setVisible(true);
JButton luru=new JButton("录入");
luru.setBounds(150, 550, 100, 50);
luru.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean mark=false;
String name=nameT.getText().trim();
String phone=phoneT.getText().trim();
if(phone.length()!=11)
{
System.out.println("您的手机号有误");
mark=true;
}
String idnum=idnumT.getText().trim();
if(idnum.length()!=18)
{
System.out.println("您的身份证号有误");
mark=true;
}//1997-10-19
String birthday=birthdayT.getText().trim();
if(birthday.indexOf(4)!='-'||birthday.indexOf(7)!='-'||birthday.indexOf(10)!='-')
{
System.out.println("您的出生日期格式有误");
mark=true;
}
if(mark)
{
System.out.println("信息录入失败");
return;
}
Operation operation=new Operation();
String userName=userNameT.getText().trim();
String sql="select * from login where username='"+userName+"'";
ResultSet resultSet=operation.query(sql);
try {
resultSet.last();
int row=resultSet.getRow();
if(!(row>0))
{
System.out.println("用户名不存在");
return;
}
else {
sql="update userinfo set name='"+name+"',phone='"+phone+"',id='"+idnum
+"',birthday='"+birthday+"' where username='"+userName+"'";
if(operation.updata(sql)>0)
System.out.println("个人信息更新成功");
else {
System.out.println("个人信息更新失败");
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
operation.close();
}
});
jFrame.add(luru);
JButton query=new JButton("查询");
query.setBounds(150, 600, 100, 50);
query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name=userNameT.getText().trim();
Operation operation=new Operation();
String sql="select * from userinfo where userName='"+name+"'";
ResultSet resultSet=operation.query(sql);
try {
ResultSetMetaData resultSetMetaData=(ResultSetMetaData) resultSet.getMetaData();
resultSet.next();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
int row;
try {
resultSet.last();
row=resultSet.getRow();
System.out.println(row);
resultSet.beforeFirst();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
System.out.println("给i奥");
if(resultSet.next())
{
String iName=resultSet.getString("name");
String iPhone=resultSet.getString("phone");
String iBirthday=resultSet.getString("birthday");
String iId=resultSet.getString("id");
System.out.println("name:"+iName);
System.out.println("phone:"+iPhone);
System.out.println("id:"+iId);
System.out.println("birthday:"+iBirthday);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
resultSet.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
operation.close();
}
});
jFrame.add(query);
jFrame.isVisible();
}
}
package Login1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Operation {
String className="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC";
String user="root";
String password="root1";
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
public ResultSet query(String sql) {
try {
Class.forName(className);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, user, password);
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
ResultSetMetaData resultSetMetaData= resultSet.getMetaData();
while(resultSet.next())
{
String kString=resultSetMetaData.getColumnName(1).toString();
System.out.println("输出:"+kString);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultSet;
}
public int updata(String sql) {
int k=0;
try {
Class.forName(className);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, user, password);
statement=connection.createStatement();
k=statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return k;
}
public void close() {
{
if(resultSet!=null)
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(statement!=null)
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(connection!=null)
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}