登陆界面你写好了把,
首先我创建一个类连接数据库的类 ConnectionToServer
首先我这个SQL是2000版本的。你应该知道怎么改成2005版本的。
我的数据库密码是sa这个根据你自己设定的来改
import java.sql.Connection; import java.sql.DriverManager;
/** * * @author xlbaby * 用来连接数据库的类 * */ public class connectionToSQL { //得到纯java驱动 public Connection getJavaPureDriver() throws Exception{ Class.forName(".microsoft.jdbc.sqlserver.SQLServerDriver"); String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=tushu"; return DriverManager.getConnection(url, "sa", "sa"); } //得到桥接驱动 public Connection getBridgeDriver() throws Exception{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:tushu"; return DriverManager.getConnection(url, "sa", "sa"); } }
然后再创建一个checkLogin类。用来检查数据库里面的数据和登录所输入的是否一样
里面读取数据库里面的数据我是用的PreparedStatement(ps)
你把这个ps里面的sql语句改下
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import connectionToServer.connectionToSQL;
/** * * @author xlbaby * 比较数据库里面的数据是否和用户登录时输入的账号密码一样 * */ public class LoginCheck { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; public boolean Check(String userName,String userPassword){ boolean tof = false; try { con = new connectionToSQL().getJavaPureDriver(); // con = new connectionToSQL().getBridgeDriver(); ps = con.prepareStatement("select userpassword from administratorwhere username = ?"); ps.setString(1, userName); rs = ps.executeQuery(); if(rs.next()){ if(rs.getString(1).equals(userPassword)) tof = true; } } catch (Exception e) { e.printStackTrace(); }finally{ try { rs.close(); ps.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } return tof; } }
然后在你登录界面上面的登陆按钮增加一个事件。当点击按钮的时候
比如说登录按钮叫submit
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//这里的UserName.getText和密码框是我自己启用的名字。你根据你的来改 if(new LoginCheck().Check(userName.getText(), new String(userPassword.getPassword()))) notice.setText("登录成功"); else notice.setText("登录失败"); } });
PIGYun发布了九月份及中秋节特惠活动,提供8折优惠码,本月商家主推中国香港和韩国机房,优惠后最低韩国每月14元/中国香港每月19元起。这是一家成立于2019年的国人商家,提供中国香港、韩国和美国等地区机房VPS主机,基于KVM架构,采用SSD硬盘,CN2+BGP线路(美国为CUVIP-AS9929、GIA等)。下面列出两款主机配置信息。机房:中国香港CPU:1core内存:1GB硬盘:10GB...
ftlcloud怎么样?ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。点击进入:ftlcloud官方网站...
近期RAKsmart上线云服务器Cloud Server产品,KVM架构1核1G内存40G硬盘1M带宽基础配置7.59美元/月!RAKsmart云服务器Cloud Server位于美国硅谷机房,下单可选DIY各项配置,VPC网络/经典网络,大陆优化/精品网线路,1-1000Mbps带宽,支持Linux或者Windows操作系统,提供Snap和Backup。RAKsmart机房是一家成立于2012年...