首页
关于java的InetAddress连接
日期:2021-10-25 11:57  点击:263
关于java的InetAddress连接

package ZY;

import java.sql.Statement;

import javax.naming.spi.DirStateFactory.Result;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Connecting {// 连接类

public static boolean Yanzheng(String username, String password) {// 此方法验证账号和密码
// 输入用户名和密码进行验证!
// 返回布尔类型
boolean flag = false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cons = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "123456");
Statement st = cons.createStatement();
ResultSet count = st.executeQuery("select * from user where user='" + username + "' and pass='" + password + "';");
flag = count.next();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("未知验证错误");
}
// System.out.println(flag);
return flag;//返回值false

}
public static void addInfoData(String username, String password) {// 向数据库添加数据
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cons = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "123456");
Statement st = cons.createStatement();

// int count=st.executeUpdate("insert into info
// values(null,"+username+","+password+")");
int count = st.executeUpdate("insert into user values('" + username + "','" + password + "')");
System.out.println(count);
} catch (Exception e) {
// TODO Auto-generated catch block
// System.out.println("未知错误");
}

}


}

=========================================================================================================================================================

package ZY;

import java.awt.Color;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;


public class EnterWindow extends JFrame {

public static boolean yan = false;
public static RegisterWindow rw;
private Panel mp = new Panel();
private JLabel user;//JLabel标签
private JLabel pass;//JLabel标签
private TextField tf1;//文本框
private JPasswordField tf2;//密码框
//定义按钮
private JButton StartButton;
private JButton registerButton;

public EnterWindow() {
this.setTitle("登录界面");
mp.setBounds(200, 200, 380, 280);//定义起始位置和大小
this.setBounds(200, 200, 380, 280);
this.setResizable(false);//固定窗口大小不可拖动
this.add(mp);
mp.setBackground(Color.white);
mp.setLayout(null);
init();
//窗体的监听事件
this.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
//监听关闭
// TODO Auto-generated method stub
}

});
}

public void init() {
//相关组件--------------------
user = new JLabel("用户:");
pass = new JLabel("密码:");
user.setBounds(80, 45, 32, 16);
pass.setBounds(80, 85, 32, 16);

tf1 = new TextField();
tf2 = new JPasswordField();
tf1.setBounds(120, 40, 200, 25);
tf2.setBounds(120, 80, 200, 25);
StartButton = new JButton("登录");
registerButton = new JButton("注册");
StartButton.setBounds(140, 120, 80, 30);
registerButton.setBounds(240, 120, 80, 30);

mp.add(StartButton);
mp.add(registerButton);
mp.add(user);
mp.add(tf1);
mp.add(pass);
mp.add(tf2);

StartButton.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {

String username = new String(tf1.getText());
String password = new String(tf2.getPassword());
if (username.equals("") || password.equals("")) {//判断账号密码是否为空
JOptionPane.showMessageDialog(null, "未检测到输入内容!", "information", JOptionPane.ERROR_MESSAGE);
} else {
yan = Connecting.Yanzheng(username, password);//向数据库发送消息 将账号和密码进行验证处理
//返回值 为true 和false

}
if (yan == true) {
JOptionPane.showMessageDialog(null, "登录成功", "information", JOptionPane.INFORMATION_MESSAGE);
Example.ex=new Example();
Example.enw.setVisible(false);//设置窗体不可见

} else {
JOptionPane.showMessageDialog(null, "账号或者密码错误", "information", JOptionPane.ERROR_MESSAGE);
}
// TODO Auto-generated method stub

}

});

registerButton.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
rw=new RegisterWindow();//调用RegisterWindow的构造方法
rw.setVisible(true);

}

});

}

}

=========================================================================================================================================================


package ZY;

import java.awt.Color;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;


public class RegisterWindow extends JFrame {
public static boolean successful = false;
public static boolean failure = false;
public static boolean registerOpen = false;
Connecting ct2 = new Connecting();
private Panel mp = new Panel();
private JLabel user;
private JLabel pass;
private TextField tf1;
private JPasswordField tf2;
private JButton StartButton;
private JButton registerButton;

public RegisterWindow() {
this.setTitle("注册界面");
mp.setBounds(400, 400, 380, 280);
this.setBounds(400, 400, 380, 280);
this.setResizable(false);
this.add(mp);
mp.setBackground(Color.orange);
// this.setLayout(null);
mp.setLayout(null);
init();
this.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
// JOptionPane.showMessageDialog(null, "不要在这里点X", "information",
// JOptionPane.ERROR_MESSAGE);
}

});
}

public void init() {

user = new JLabel("用户:");
pass = new JLabel("密码:");
user.setBounds(80, 45, 32, 16);
pass.setBounds(80, 85, 32, 16);

tf1 = new TextField();
tf2 = new JPasswordField();
tf1.setBounds(120, 40, 200, 25);
tf2.setBounds(120, 80, 200, 25);
registerButton = new JButton("提交");
registerButton.setBounds(180, 120, 80, 30);

mp.add(registerButton);
mp.add(user);
mp.add(tf1);
mp.add(pass);
mp.add(tf2);

registerButton.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
@SuppressWarnings("deprecation")
String username = new String(tf1.getText());
String password = new String(tf2.getPassword());
if (username.equals("") && !password.equals("")) {
JOptionPane.showMessageDialog(null, "未检测到用户名输入", "information", JOptionPane.ERROR_MESSAGE);
} else if (!username.equals("") && password.equals("")) {
JOptionPane.showMessageDialog(null, "未检测到密码输入", "information", JOptionPane.ERROR_MESSAGE);
} else if (username.equals("") && password.equals("")) {
JOptionPane.showMessageDialog(null, "请输入内容!!", "information", JOptionPane.ERROR_MESSAGE);
} else {
Connecting.addInfoData(username, password);
}

if (registerOpen == true) {
JOptionPane.showMessageDialog(null, "此用户已存在 请重新注册", "information", JOptionPane.ERROR_MESSAGE);
System.exit(0);
// EnterWindow.successful=true;

} else if (!username.equals("") && !password.equals("")) {
JOptionPane.showMessageDialog(null, "注册成功", "information", JOptionPane.INFORMATION_MESSAGE);
EnterWindow.rw.setVisible(false);
}

}

});

}
}

=====================================================================================================================================================

package ZY;

import java.awt.*;
import java.awt.event.*;
import java.io.Reader;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import java.sql.*;

public class Example extends JFrame {
//定义类的变量
public static Example ex;
public static EnterWindow enw;
//定义窗体变量
private JButton reg;//注册按钮
private JButton show;//查看按钮
private JButton edit;//编辑按钮
private JButton del;//删除按钮
private JPanel up;
private JPanel down;
private JLabel uptext;
private JTextField user;//账号框
private JTextField pass;//密码框
private JLabel result;
private JButton editpass=new JButton("修改密码");//提交改密按钮
private JButton dell=new JButton("删除用户");//提交删除按钮
private Map usermap=new HashMap();
//定义数据库变量
Statement stmt = null;
Connection conn = null;
ResultSet rs = null;
//查看
public void show() {
Object[] tabletop= {"用户名","密码"};
Object[][] tableuser= new Object[usermap.size()][];//创建二维数组
Setuserkey=usermap.keySet();
int i=0;
for(String user:userkey) {//用foreach循环将userkey的值赋值给user
tableuser[i]=new Object[]{user,usermap.get(user)};
i++;
}
tableuser=readuser();
JTable table = new JTable(tableuser,tabletop);//创建表并传入tableuser,tabletop
JDialog dialog = new JDialog(Example.this, true );
dialog.setTitle("显示用户名");
dialog.setSize(300, 200);//窗体大小
dialog.setLocation(50, 50);//位置
dialog.add(table);//将表格加入窗体
dialog.setVisible(true);//窗体可视
}
//修改
void edit() {
JDialog dialog = new JDialog(Example.this, true );
dialog.setLayout(new GridLayout(3,2));//设置布局
dialog.setTitle("用户修改");
dialog.setSize(300, 200);
dialog.setLocation(50, 50);
JLabel usertext=new JLabel("用户名");
JLabel passtext=new JLabel("密码");
user=new JTextField();
pass=new JTextField();
result=new JLabel();
dialog.add(usertext);//添加按钮及文本框
dialog.add(user);//添加按钮及文本框
dialog.add(passtext);//添加按钮及文本框
dialog.add(pass);//添加按钮及文本框
dialog.add(editpass);//添加按钮及文本框
dialog.add(result);//添加按钮及文本框

dialog.setVisible(true);//窗体可视
}
//删除
void del() {
JDialog dialog = new JDialog(Example.this, true );
dialog.setLayout(new GridLayout(3,2));
dialog.setTitle("用户删除");
dialog.setSize(300, 200);
dialog.setLocation(50, 50);
JLabel usertext=new JLabel("用户名");
JLabel passtext=new JLabel("密码");
user=new JTextField();
pass=new JTextField();
result=new JLabel();
//将上面的按钮和文框添加进去
dialog.add(usertext);
dialog.add(user);
dialog.add(passtext);
dialog.add(pass);
dialog.add(dell);
dialog.add(result);

dialog.setVisible(true);//窗体可呈现出来
}
//数据库的coon
void conn() {
try {
// 1. 注册数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.通过DriverManager获取数据库连接
String url = "jdbc:mysql://localhost:3306/java";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
// 3.通过Connection对象获取Statement对象
stmt = conn.createStatement();
// 4.使用Statement执行SQL语句。
} catch (Exception e) {
e.printStackTrace();
}
}
//数据库的回收
void closedb() {
// 6.回收数据库资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public Example() {
//窗体
JFrame f=new JFrame("注册窗口");
f.setSize(300,200);
f.setLocation(300,200);
show=new JButton("查看");//查看按钮
edit=new JButton("修改") ;//编辑按钮
del=new JButton("删除");//删除按钮
f.setLayout(new BorderLayout());//布局
up=new JPanel() ;
down=new JPanel();
f.add(up,BorderLayout.NORTH);//边框布局——北
f.add(down,BorderLayout.SOUTH);//边框布局——南
down.add(show);//添加查看按钮
down.add(edit);//添加编辑按钮
down.add(del);//添加删除按钮
uptext=new JLabel("欢迎进入!");//发送欢迎辞
uptext.setFont(new Font("宋体",Font.PLAIN,25));//设置文本字体
up.add(uptext);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//菜单
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("操作");
JMenuItem item2 = new JMenuItem("查看用户");//将注册用户赋予给item2
JMenuItem item3 = new JMenuItem("修改用户");//将注册用户赋予给item3
JMenuItem item4 = new JMenuItem("删除用户");//将注册用户赋予给item4

f.setJMenuBar(menuBar);
menuBar.add(menu);
menu.add(item2);//将item2加入
menu.add(item3);//将item3加入
menu.add(item4);//将item4加入
f.setVisible(true);
//查看用户名
show.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
show();
}
});
//菜单查看用户名
item2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
show();

}
});
//按钮修改密码
edit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
edit();

}
});
//菜单修改密码
item3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
edit();

}
});
//按钮删除监听
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
del();

}
});
//菜单删除监听
item4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
del();

}
});


//提交修改密码监听
editpass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String usersave=user.getText();//获取用户文本框
String passave=pass.getText();//获取密码文本框
if(usermap.size()>0){
Setuserkey=usermap.keySet();
for(String user:userkey) {
if(!usersave.equals(user)) {
usermap.put(usersave,passave);
result.setText("修改密码成功");
update(usersave,passave);
}else {
result.setText("密码重复");
}
}
}else {
usermap.put(usersave,passave);
result.setText("修改密码成功");
update(usersave,passave);
}
}

private void update(String usersave, String passave) {
// TODO 自动生成的方法存根
}
});
//删除账户与密码监听
dell.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String usersave=user.getText();
String passave=pass.getText();
if(usermap.size()>0){
Setuserkey=usermap.keySet();
for(String user:userkey) {
if(!usersave.equals(user)) {
usermap.put(usersave,passave);
result.setText("删除成功");
del(usersave,passave);
}else {
result.setText("不要重复删除");
}
}
}else {
usermap.put(usersave,passave);
result.setText("删除成功");
del(usersave,passave);
}
}

});
}

//读取数据库用户
Object[][] readuser() {
Object[][] tableuser = null;
try {
conn();
int count=0;
String sqlall = "select count(*) as result from user";
rs = stmt.executeQuery(sqlall);
while(rs.next())
{
count = rs.getInt("result");
}
tableuser= new Object[count][];
String sql = "select * from user";
rs = stmt.executeQuery(sql);
int i=0;
while (rs.next()) {
String user = rs.getString("user");
String pass = rs.getString("pass");
tableuser[i]=new Object[]{user,pass};
i++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closedb();
}
return tableuser;
}
//插入数据库用户
void insertuser(String user,String pass) {
try {
conn();
String sql = "INSERT INTO user(user,pass) value('"+user+"','"+pass+"')";//插入数据库语句
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
closedb();
}
}

//修改数据库
void editpass(String user,String pass) {
try {
//1. 注册数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManager获取数据库连接
String url = "jdbc:mysql://localhost:3306/java";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
//3.通过Connection对象获取Statement对象
stmt = conn.createStatement();
//4.使用Statement执行SQL语句。
String sql = "update user set pass='"+pass+" where 'user='"+user+"'";//修改数据库语句
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
closedb();
}

}
//删除数据库用户
void del(String user,String pass) {
try {
//1. 注册数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManager获取数据库连接
String url = "jdbc:mysql://localhost:3306/java";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
//3.通过Connection对象获取Statement对象
stmt = conn.createStatement();
//4.使用Statement执行SQL语句。
String sql = "delete from user where user='"+user+"' and pass='"+pass+"'";//删除账户和密码sql语句
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
closedb();
}

}
public static void main(String[] aa) {
enw=new EnterWindow();//调用这个类里面的对象和方法
enw.setVisible(true);//可视化
}
}






上一篇· java聊天室案例    下一篇·java的Socket 连接
最新文章
热门文章
关于网站  |  联系我们  |  网站地图  | 
2021-10-25 11:57
Powered by edu63.com
首页 刷新 顶部