我帮你写了一个最简单的、满足了你的要求的项目。登录界面中,输入仓库名字,然后连接数据,查询是否存在改仓库名,若存在,则向主窗芦余慎体跳转,毁码若不存在,则提示仓库名不存在。
代码如下:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JTextField;
import javax.swing.JButton;
public class MySJK extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public MySJK() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("登录");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(30, 16, 130, 18));
jLabel.setText("陪敬请输入要登录的仓库:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(30, 45, 194, 27));
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(149, 87, 74, 26));
jButton.setText("登录");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String myckstr=jTextField.getText();
String checkstr="false";
Connection con=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:CANGKU","","");
Statement sta=con.createStatement();
ResultSet rs=sta.executeQuery("select * from 仓库 where ckid='"+myckstr+"'");
if(rs.next()){
checkstr="true";
}
rs.close();
sta.close();
}
catch(Exception ee){
ee.printStackTrace();
}
finally{
try{
con.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
if(checkstr.equals("true")){
////登录系统的主窗体
}
else{
/////提示仓库名不存在
}
}
});
}
return jButton;
}
public static void main(String args[]){
new MySJK();
}
}
////记得给我分哦!^_^
你写一个UserIsExist方法,方信灶法里前虚面去数据库里查那个用户名的user,如果有就返回false如果没有就返回true。调用方根据返回值来滑悔扮做相关操作或判断
private boolean checkUser(String name, String pwss) {
String userName = "";
Statement stmt = null;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT username FROM userinfo WHERE username = '" + name + "尺敏' AND password = '让困丛" + pwss +"'");
// 如果有结果集,就拿到用户名
if (rs.next()) {
userName = rs.getString(1).trim();
}
stmt.close();
// 如坦樱果用户名为空,说明不存在该用户或是密码不正确
if (userName.equals("")) {
return false;
} else {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
你要获取数据库的连团伏接,然后写
创建 statement
String sql = "select * from users where account = '"+accountTextField.getText()+"' and password='"+passwordTextField.getText()+"'";
然后执行statement.excuseQuery(sql),
饭回来的是个ResultSet
然后
boolean exist = false;//是否存在的标识
while(rs.next){//循环结果集模或败(如果有下一个,那么说明用户存在)
exist = true;//设置为存在这个用户
break;//跳出循环
}
最后面判断exist是不是true ,如果是true就说明存在,如果为false就说明旦颤不存在‘
伪代码,无法直接运行,但是过程是这样的。稍加修改即可
比如数据拍磨库的表叫student
select * from student where name = ** and pass_word = **
**就是你输入的东和困西,用作数据库查询的条件,然后判断,如果查到的结果为空,则提示账号袭棚斗或者密码错误,否则登陆成功