数据 库连接的代码:
import java.sql.*;
public class db_manager{
ResultSet rs;
String strurl="jdbc:odbc:kehudata";
db_manager(){
}
//查询方法,返回查询结果集
public ResultSet getResult(String sql){
try{
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
//创建连接
Connection conn=DriverManager.getConnection(strurl,"","") ;
//创建statement对象
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//执行SQL语句,返回结果集
ResultSet rs=stmt.executeQuery(sql);
//返回
return rs;
}
//异常处理
catch(Exception e){
System.out.println( "getResult------"+e.toString()); //若有异常,返回null
return null;
}
}
//执行更新,删除语句方法
public boolean executeSql(String sql){
try{
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//创建连接
Connection conn=DriverManager.getConnection(strurl);
//创建statement对象
Statement stmt=conn.createStatement();
//执行SQL语句,返回结果集
stmt.executeUpdate(sql);
//提交到数据库
conn.commit();
//返回true,表示操作成功
return true;
}
catch(Exception e){
System.out.println( "executeSql------"+e.toString() );
//返回false,表示操作失败
return false;
}
}
}
//管理员登陆的代码
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import mypackage.flag.myflag;
public class manager_login extends JFrame{
myflag f=new myflag();
ResultSet rs;
String sql;
JLabel jusername;
JLabel jpassword;
JTextField username;
JPasswordField password;
JButton jb1,jb2;
db_manager db=new db_manager();
private void make_GUI(){
jusername=new JLabel("用户名* ");
jpassword=new JLabel("密码* ");
jb1=new JButton("确定");
jb2=new JButton("取消");
username=new JTextField(10);
password=new JPasswordField(10);
Container cp=getContentPane();
JPanel jp=new JPanel();
JPanel jp2 =new JPanel();
jp.setLayout(new FlowLayout());
jp.add(jusername);jp.add(username);
jp.add(jpassword);jp.add(password);
jp2.add(jb1);
jp2.add(jb2);
cp.add(jp,BorderLayout.NORTH);
cp.add(jp2,BorderLayout.SOUTH);
setSize(400,200);
setVisible(true);
}
private void action(){
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sql="select * from admin where admin='"+username.getText().trim()+"' and password='"+f.password_safe(password.getText().trim())+"'";
rs=db.getResult(sql);
try{
if(rs.next()) {
JOptionPane.showMessageDialog(null,"登陆成功!");
f.username=rs.getString("admin");
f.flag=true;
exit();
}
else {JOptionPane.showMessageDialog(null,"用户名或密码错误!");}
}catch(SQLException q){
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
exit();
}
});
}
public void exit(){
this.dispose();
}
manager_login(){
super("管理员登陆");
make_GUI();
action();
}
public static void main(String[] args){
new manager_login();
}
}
书籍,电影,音乐的互动交流平台http://www./