*连接SQL2005数据库,JDBC连接方式,1280×1024分辨率下运行效果最佳*超级用户:Administrator 密码:111。普通用户:111 密码:111 进入系统均可修改密码,超级用户不可被删除,普通用户可以被删除且不具有添加删除用户功能*主要设计人员:* Kevin.S *在SLQ2000中使用时需注意:1.修改数据库用户名密码  2.将三个包添加到JCreator中,*    3.将String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"。改为*  String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"。*/ import java.sql.*。import java.awt.*。import javax.swing.*。import java.awt.event.*。import javax.swing.border.*。import javax.swing.JOptionPane。import javax.swing.JLabel。import javax.swing.table.DefaultTableModel。import javax.swing.table.TableColumn。import java.awt.event.MouseAdapter。
//创建登陆界面类class LoginFrame extends JFrame {
public static final int WIDTH = 350。public static final int HEIGHT = 160。private static JTextField n_username。private static JPasswordField n_pwd。private JButton loginBtn。private JButton cacelBtn。private String admin="Administrator"。private boolean IsAdmin=false。public LoginFrame(> {
setTitle("欢迎使用SKY学生信息管理系统">。
setSize(WIDTH,HEIGHT>。
setLocation(450,350>。
ImageIcon ic = new ImageIcon("src/icon.JPG">。
Image(>>。
Container contentPane = getContentPane(>。
//建立容器面板
JPanel textPanel = new JPanel(>。
//新建用户名标签
JLabel username = new JLabel("用户名">。
username.setVerticalAlignment(1>。//TOP
username.setHorizontalAlignment(2>。//LEFT n_username = new JTextField("Administrator",10>。//添加默认超级用户登陆n_username.setHorizontalAlignment(JTextField.CENTER>。
username.setHorizontalAlignment(4>。//RIGHT
textPanel.add(username>。java连接sqlserver数据库
textPanel.add(n_username>。
//新建密码标签JLabel password = new JLabel("密码">。
password.setVerticalAlignment(0>。//CENTER
password.setHorizontalAlignment(2>。
n_pwd = new JPasswordField(10>。
password.setHorizontalAlignment(JTextField.CENTER>。
password.setHorizontalAlignment(4>。
textPanel.add(password>。
textPanel.add(n_pwd>。
//创建按钮loginBtn = new JButton("登陆",new ImageIcon("src/login.JPG">>。
cacelBtn = new JButton("取消",new ImageIcon("src/cancel.gif">>。
//注册
loginBtn.addActionListener(new LoginAction(>>。
cacelBtn.addActionListener(new LoginAction(>>。
textPanel.add(loginBtn>。
textPanel.add(cacelBtn>。
contentPane.add(textPanel>。}
//实现private class LoginAction implements ActionListener {
public void actionPerformed(ActionEvent e>
{ //如果点击登陆
Source(> == loginBtn>
{ //获取用户名域和密码域内容
if(!LoginSucess(>>
{ JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!", "错误",
JOptionPane.ERROR_MESSAGE>。
n_username.setText("">。
n_pwd.setText("">。
return。
}
else
{ //调用主界面MFrame mf = new MFrame(IsAdmin>。
closeWindow(>。
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE>。
mf.show(>。
}
}
else
{ it(0>。//退出
}
} }
//返回登陆成功与否boolean LoginSucess(> {
if(ConnectSQL(>>
return true。
return false。}
//登陆成功后关闭登陆界面void closeWindow(> {
this.dispose(>。}
//连接数据库boolean ConnectSQL(> { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"。//加载JDBC驱动  String dbURL = "jdbc:sqlserver://localhost:1433。 DatabaseName=JavaTest "。 //连接服务器和数据库sample
String userName = "sa"。//默认用户名String userPwd = "songyuejie"。//密码String getname = Text(>。
if(getname.equals(admin>> IsAdmin = true。//设置管理员登陆模式char[] pwd = Password(>。
String getpwd = new String(pwd>。
String sql = "select * from 登陆where 用户名='"+getname+"' and 密码='"+getpwd+"' "。
Connection dbConn。
try {
Class.forName(driverName>。
dbConn = Connection(dbURL, userName, userPwd>。
//通过Statement查询记录
Statement stmt = ateStatement(>。
//取得返回的记录集ResultSet rs = uteQuery(sql>。
(>>
{
return true。
}
stmt.close(>。
rs.close(>。
return false。
} catch (Exception e> {
e.printStackTrace(>。
return false。
} }
}
//添加用户界面class UserAdd extends JDialog implements ActionListener {
JTextField field1。JPasswordField field2,field3。JPanel panel1,panel2,panel3。JLabel label,label1,label2,label3。JButton confBtn,cancelBtn。public UserAdd(> {
setTitle("添加用户">。
Container container=getContentPane(>。
container.setLayout(new FlowLayout(>>。
setLocation(400,400>。
setSize(520,200>。
setVisible(true>。
ImageIcon ic = new ImageIcon("src/icon.JPG">。
Image(>>。
panel1=new JPanel(>。
panel2=new JPanel(>。
panel3=new JPanel(>。
label=new JLabel("请输入你要添加的用户信息">。
label1=new JLabel("新用户名">。
field1=new JTextField("",10>。
label2=new JLabel("密码">。
field2=new JPasswordField("",10>。
label3=new JLabel("确认密码">。
field3=new JPasswordField("",10>。
confBtn=new JButton("添加">。
cancelBtn=new JButton("取消">。
panel1.add(label>。
panel2.add(label1>。
panel2.add(field1>。
panel2.add(label2>。
panel2.add(field2>。
panel2.add(label3>。
panel2.add(field3>。
panel3.add(confBtn>。
panel3.add(cancelBtn>。
container.add(panel1,BorderLayout.NORTH>。
container.add(panel2,BorderLayout.SOUTH>。
container.add(panel3>。
confBtn.addActionListener(this>。
cancelBtn.addActionListener(this>。}
void closeDialog(> {
this.dispose(>。//关闭该对话框}
public void actionPerformed(ActionEvent e>
{ Source(> == confBtn>//添加
{
if(AddUserSucess(>>
{ JOptionPane.showMessageDialog(null, "添加成功", "提示", 1>。
field1.setText("">。
field2.setText("">。