import java.awt.Container;
我们提供的服务有:网站设计制作、网站设计、微信公众号开发、网站优化、网站认证、四川ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的四川网站制作公司
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.*;public class Login extends JFrame{
JLabel user,passwd;
JTextField userput;
JPasswordField passput;
JButton denglu,tuichu;
public Login(){
super("用户登录");
Container c=getContentPane();
c.setLayout(null);
Font f=new Font("宋体",Font.PLAIN,12);
user=new JLabel("账号");
passwd=new JLabel("密码");
userput=new JTextField();
passput=new JPasswordField();
denglu=new JButton("登录");
denglu.setFont(f);
denglu.addActionListener(new NewAction());
tuichu=new JButton("退出");
tuichu.setFont(f);
tuichu.addActionListener(new NewAction());
user.setBounds(50,50,60,20);
userput.setBounds(110,50,150,20);
passwd.setBounds(50,80,60,20);
passput.setBounds(110,80,150,20);
denglu.setBounds(50,160,60,30);
tuichu.setBounds(200,160,60,30);
c.add(user);
c.add(userput);
c.add(passwd);
c.add(passput);
c.add(denglu);
c.add(tuichu);
setSize(350, 300);
setVisible(true);
}
class NewAction implements ActionListener{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CDM";
String user="sa";
String passwd="394513265";
java.sql.Connection con;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con= DriverManager.getConnection(url,user,passwd);
}catch(Exception ep){
JOptionPane.showMessageDialog(null, "加载驱动失败!");
}
if(e.getSource()==denglu){
Find();
}
if(e.getSource()==tuichu){
dispose();
}
} public void Find(){
String lk="select * from login";
try{
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(lk);
while(rs.next()){
if(rs.getString(1).equals(userput.getText()) rs.getString(2).equals(passput.getText()))
new MainClient();
else
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
rs.close();
}catch(SQLException p){
JOptionPane.showMessageDialog(null, p.getMessage());
}
}
}
public static void main(String[] args) {
Login l=new Login();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
步骤就是建个工程 然后建个class
同意楼上的说法,具体点可以这样:创建一个用户表,里边包括LoginName(登录名),UserName(用户名),Password(密码),Age(年龄),Address(地址)。然后编写Java程序(用MVC架构)模型层(M):DBConnection.java(负责连接数据库)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
public class DBConnection {
private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String DB_URL = "jdbc:odbc:text";
public DBConnection() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DB_URL);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
return conn;
}
}
第2个负责数据库查询操作的类:DBUserManager.java
import edu.systop.text.model.entity.User;
import edu.systop.text.model.dao.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.*;
public class DBUserManager {
private static final String SQL_SELECT =
"SELECT LoginName,UserName,PassWord,Age,Address FROM UserInfo WHERE LoginName = ? AND PassWord = ?";
public DBUserManager() {
}
public boolean checkDB(User u) {
boolean b = false;
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
conn = DBConnection.getConnection();
try {
psmt = conn.prepareStatement(SQL_SELECT);
psmt.setString(1, u.getLoginName());
psmt.setString(2, u.getPassWord());
rs = psmt.executeQuery();
b = rs.next();
if (rs.next()) {
b = true;
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
cleanDB(rs, psmt, conn);
}
return b;
}
public User checkBC(User u) {
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
User tmp = new User();
conn = DBConnection.getConnection();
try {
psmt = conn.prepareStatement(SQL_SELECT);
psmt.setString(1, u.getLoginName());
psmt.setString(2, u.getPassWord());
rs = psmt.executeQuery();
if (rs.next()) {
tmp.setLoginName(rs.getString(1));
tmp.setUserName(rs.getString(2));
tmp.setAge(rs.getInt(4));
tmp.setAddress(rs.getString(5));
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
cleanDB(rs, psmt, conn);
}
return tmp;
}
public void cleanDB(ResultSet rs, PreparedStatement psmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
第3个实体用户类:User.java
package edu.systop.text.model.entity;
public class User {
private String loginName;
private String userName;
private String passWord;
private int age;
private String address;
public User() {
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public String getLoginName() {
return loginName;
}
public String getUserName() {
return userName;
}
public String getPassWord() {
return passWord;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
然后编写控制层(C):GetInfoServlet.java
package edu.systop.text.control;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import edu.systop.text.model.entity.User;
import edu.systop.text.model.service.UserManager;
public class GetInfoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String loginName = request.getParameter("loginName");
String passWord = request.getParameter("passWord");
User u = new User();
u.setLoginName(loginName);
u.setPassWord(passWord);
UserManager m = new UserManager();
RequestDispatcher d;
if (m.checkUser(u)) {
User o = m.checkBC(u);
request.setAttribute("JavaBEAN",o);
d = request.getRequestDispatcher("GetInfoUser.jsp");
} else {
d = request.getRequestDispatcher("GetInfoFinale.jsp");
}
d.forward(request, response);
}
//Clean up resources
public void destroy() {
}
}
最后,创建表示层(V):包括3个Jsp(登录页面GetInfo.jsp、登录成功页面GetInfoUser.jsp、登录失败页面GetInfoFinale.jsp)
上面的就是Jsp结合Servlet用MVC架构写的用户登录程序。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用户名");
JLabel lbl2=new JLabel("密码");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陆");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陆成功!");
}
else
JOptionPane.showMessageDialog(null,"用户名或密码错误!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test26 {
public static void main(String[] args) {
final String userName = "abc";
final String passwrod = "123";
JFrame jFrame = new JFrame("登陆界面");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
jFrame.add(label1);
JLabel label2 = new JLabel("密码");
label2.setBounds(10, 40, 100, 30);
jFrame.add(label2);
final JTextField text1 = new JTextField();
text1.setBounds(50, 15, 130, 20);
jFrame.add(text1);
final JPasswordField text2 = new JPasswordField();
text2.setBounds(50, 45, 130, 20);
jFrame.add(text2);
JButton button = new JButton("Login");
button.setBounds(10, 75, 170, 40);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(userName.equals(text1.getText()) passwrod.equals(text2.getText())) {
JOptionPane.showMessageDialog(null, "登陆成功误", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一个微信公众号,经常会分享一些Java技术相关的干货,还有一些学习资源。
如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注。
你要先学会截图哦,你发的看不清楚,重新写了一个你参考参考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Day30A extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
private ButtonGroup btg;
private JRadioButton jr1,jr2;
Day30A(){
this.setTitle("注册账户");
this.setLayout(new GridLayout(7,1));
this.setSize(300,280);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
String str="卡片类型1,卡片类型2,卡片类型3,卡片类型4,卡片类型5";
jcb=new JComboBox(str.split(","));
labelId=new JLabel("账号: ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密码: ");
labelMoney=new JLabel("开户金额:");
labelSelect=new JLabel("存款类型:");
labelCar=new JLabel("卡片类型:");
addFun1();
addFun2();
}
private void addFun2() {
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
}
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
jp1.add(new JTextField(15));
jp2=new JPanel();
jp2.add(labelName);
jp2.add(new JTextField(15));
jp3=new JPanel();
jp3.add(labelPass);
jp3.add(new JTextField(15));
jp4=new JPanel();
jp4.add(labelMoney);
jp4.add(new JTextField(13));
jp5=new JPanel();
jp5.add(labelSelect);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
jr2=new JRadioButton("活期",true);
btg.add(jr1);
btg.add(jr2);
jp5.add(jr1);
jp5.add(jr2);
jp6=new JPanel();
jp6.add(labelCar);
jp6.add(jcb);
jp7=new JPanel();
jp7.add(new JButton("确定"));
jp7.add(new JButton("取消"));
}
public static void main(String[] args) {
new Day30A();
}
}