开发者社区> 问答> 正文

java制作登陆界面验证问题mysql为数据库:操作报错

就是登陆界面点击登陆验证用户名和密码然后进入下一个界面的代码仅仅需要一个按钮的就好= =!数据库已经测试连接成功 附数据库和两个界面的代码
这个是连接代码

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionDemo02{ //public ConnectionDemo02 conn; public static final String DBDRIVER="org.gjt.mm.mysql.Driver"; public static final String DBURL="jdbc:mysql://localhost:3306/wxf"; public static final String DBUSER="root"; public static final String DBPASS="26533621"; public static void main(String[] args){ Connection conn=null; try{ Class.forName(DBDRIVER); }catch (ClassNotFoundException e){ e.printStackTrace(); } try{ conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); }catch(SQLException e){ e.printStackTrace(); } System.out.print(conn); try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } }
这是登陆界面
import java.awt.; import java.awt.event.; import java.sql.; import javax.swing.; import java.awt.geom.*; import java.util.Vector; public class denglu extends JFrame { public Label name=new Label("用户名"); public Label pass=new Label("密码"); public TextField txtname=new TextField(); public TextField txtpass=new TextField(); public Button btok=new Button("登陆"); public Button btexit=new Button("取消"); public denglu() { setTitle("欢迎使用工资管理系统"); setLayout(null);

   setResizable(false);
   setSize(500,350);
  
   Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frm=this.getSize();
   setLocation((scr.width-frm.width)/2,(scr.height-frm.height)/2-18);
   txtpass.setEchoChar('*');
   name.setBounds(70,260,40,27);
   pass.setBounds(70,300,40,27);
   txtname.setBounds(120,260,120,27);
   txtpass.setBounds(120,300,120,27);
   btok.setBounds(340,260,100,28);
   btexit.setBounds(340,300,100,28);
   add(name);
   add(txtname);
   add(pass);
   add(txtpass);
   add(btok);
   add(btexit);
   setVisible(true);
   }
public static void main(String args[])
{
	denglu dl=new denglu();
}

}

这个是点击后进入的界面代码
import java.awt.; import java.awt.event.; import java.awt.geom.; import javax.swing.; import javax.swing.event.; import java.sql.; public class yonghu { public static void main(String args[]) { JFrame jf=new  JFrame(); jf.setTitle("aaa"); jf.setBounds(300,250,300,200); jf.setVisible(true); } }

 

展开
收起
kun坤 2020-06-04 13:19:20 1067 0
1 条回答
写回答
取消 提交回答
  • import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class ConnectionDemo02{
                   //public ConnectionDemo02 conn;
    	public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
    	public static final String DBURL="jdbc:mysql://localhost:3306/wxf";
    	public static final String DBUSER="root";
    	public static final String DBPASS="26533621";
    	public  Connection getConn(){
    		Connection conn=null;
    		try{
    			Class.forName(DBDRIVER);
    		}catch (ClassNotFoundException e){
    			e.printStackTrace();
    		}
    		try{
    			conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    		
    		return conn;
    	}
    }
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class Login {
    	PreparedStatement ps = null;
    	ResultSet rs = null;
    	Connection conn = null;
    
    	public boolean verify(String name,String password ) {
    		boolean result=false;
    		
    		String sql = "select * from usekey where idcard=? and password=?";
    		Connection con = new ConnectionDemo02().getConn();
    		try {
    			ps = con.prepareStatement(sql);
    			ps.setString(1, name);
    			ps.setString(2, password);
    			rs = ps.executeQuery();
    			
    			if (rs.next()) {//验证成功
    				result=true;
    			}
    
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				if (rs != null)
    					rs.close();
    				if (ps != null)
    					ps.close();
    				if (conn != null)
    					conn.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    		return result;
    	}
    
    }
    
    ######非常感谢感谢 再感谢 已经成功了 有点小问题已经完成 谢谢######回复 @wangms : 写在一个里面也没问题,不过面向对象讲究类的重复利用,最好按业务功能的不同写到不同的类中######哦 这个验证是不是要新用一个 不能再原来里面加是吧0.0###### 高手们 没人知道么 帮帮小弟啊 卡了快1周了 ######首先我明白为什么有三个main方法,一个java程序main方法是一个入口,应该只有一个,对于这里来讲main方法就应该是new出你的登录界面denglu dl=new  denglu();了,另外你没有给登录这个button注册事件,他当然什么都不做了,大致的流程是这样,main中new出登录窗口,登录按钮注册点击事件,然后在注册的方法中连接数据库检测帐号和密码是否与用户输入的匹配,如果正确就展示用户界面。######这个我知道 我就是写了监听了 一直错 然后一气之下都删掉了 然后看看能不能帮我写一段对的 只是确定的监听验证账号密码代码就好######
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.util.Vector;
    
    public class denglu extends JFrame {
    	public Label name = new Label("用户名");
    	public Label pass = new Label("密码");
    	public TextField txtname = new TextField();
    	public TextField txtpass = new TextField();
    	public Button btok = new Button("登陆");
    	public Button btexit = new Button("取消");
    
    	public denglu() {
    		setTitle("欢迎使用工资管理系统");
    		setLayout(null);
    
    		setResizable(false);
    		setSize(500, 350);
    
    		Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
    		Dimension frm = this.getSize();
    		setLocation((scr.width - frm.width) / 2,
    				(scr.height - frm.height) / 2 - 18);
    		txtpass.setEchoChar('*');
    		name.setBounds(70, 260, 40, 27);
    		pass.setBounds(70, 300, 40, 27);
    		txtname.setBounds(120, 260, 120, 27);
    		txtpass.setBounds(120, 300, 120, 27);
    		btok.setBounds(340, 260, 100, 28);
    		btexit.setBounds(340, 300, 100, 28);
    		add(name);
    		add(txtname);
    		add(pass);
    		add(txtpass);
    		add(btok);
    		add(btexit);
    		setVisible(true);
    		btok.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				if (验证用户名和密码通过) {
    					denglu.this.dispose();
    					new yonghu().init();
    				}
    			}
    		});
    	}
    
    	public static void main(String args[]) {
    		denglu dl = new denglu();
    	}
    }
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.sql.*;
    
    public class yonghu {
    	public void init() {
    		JFrame jf = new JFrame();
    		jf.setTitle("aaa");
    		jf.setBounds(300, 250, 300, 200);
    		jf.setVisible(true);
    	}
    }
    ######回复 @wangms : 下面那个Login类,if(new Login().verify(name,password))######就是jdbc的操作,你需要熟悉下jdbc的操作,熟悉了,就自然知道该怎么搞了######验证用户名和密码通过.....亲 就是这段= =!关键不是要汉子啊...........
    2020-06-05 14:28:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像