求助,java连接sql server2008出错!com.microsoft.jdbc.sqlserver.SQLServerDriver

mjx320382 2016-03-26 10:41:05

附代码:

数据库连接类
package classsource;

import java.sql.*;

public class Database {
public static Connection cn;
public static Statement st;
public static ResultSet rs;

public static boolean joinDB() {
boolean joinFlag;
try {
joinFlag = true;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=EmployeeInformationMS","sa","admin12");

cn.setCatalog("EmployeeInformationMS");
System.out.println("数据库连接成功");
st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return joinFlag;

} catch (SQLException sqlEx) {
System.out.println(sqlEx.getMessage());

joinFlag = false;
return joinFlag;

} catch (ClassNotFoundException notfoundEX) {
System.out.println(notfoundEX.getMessage());

joinFlag = false;
return joinFlag;
}
}

public static boolean executeSQL(String sqlString) {
boolean executeFlag;
try {
st.execute(sqlString);
executeFlag = true;
} catch (Exception e) {
executeFlag = false;
System.out.println("sql exception:" + e.getMessage());
}
return executeFlag;
}


public static boolean query(String sqlString) {

try {
rs = null;
//System.out.println(sqlString);
rs = st.executeQuery(sqlString);
} catch (Exception Ex) {
System.out.println("sql exception:" + Ex);
return false;
}
return true;
}
}

//用户登陆类


package classsource;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Land extends JFrame{
JFrame jf ;

JTextField textName=new JTextField();
JPasswordField textage=new JPasswordField();

JLabel label = new JLabel("员工管理系统");
JLabel labelName=new JLabel("用户名:");
JLabel labelage=new JLabel("密码:");


JButton buttonEnter=new JButton("登录");
JButton buttoncancel=new JButton("清空");

public Land(){
jf=this;
setTitle("登录");
Font f = new Font("新宋体",Font.PLAIN,12);
Container con = getContentPane();
con.setLayout(null);
label.setBounds(95,10,110,20);
label.setFont(new Font("新宋体",Font.PLAIN,14));
con.add(label);
labelName.setBounds(45,40,55,20);
labelName.setFont(f);
con.add(labelName);
textName.setBounds(95,40,120,20);
con.add(textName);

labelage.setBounds(45,70,45,20);
con.add(labelage);
labelage.setFont(f);
textage.setBounds(95,70,120,20);
con.add(textage);



buttonEnter.setBounds(90,110,60,20);
buttonEnter.setFont(f);
con.add(buttonEnter);

//登陆的鼠标监听

buttonEnter.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
if(textName.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用户名不能为空!");
}
else if(textage.getText().equals("")){
new JOptionPane().showMessageDialog(null,"密码不能为空!");
}
else{
String sql="select * from UserInformation where User_Name = '" + textName.getText() + "' and Password = '" + textage.getText()+ "'";
System.out.println(sql);
Judge(sql);
}
}
});

buttoncancel.setBounds(155,110,60,20);
buttoncancel.setFont(f);
con.add(buttoncancel);

//清空按钮的鼠标监听方法
buttoncancel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
textName.setText("");
textage.setText("");
}
});

setResizable(false);
//窗口图标
Image img=Toolkit.getDefaultToolkit().getImage("image\\main.gif");
setIconImage(img);
Toolkit t = Toolkit.getDefaultToolkit();
int w = t.getScreenSize().width;
int h = t.getScreenSize().height;
setBounds(w/2-150,h/2-90,300,180);
setVisible(true);
}

private void Judge(String sqlString) {

if (Database.joinDB()) {
if (Database.query(sqlString))
try{
if(Database.rs.isBeforeFirst()) {
System.out.println("密码正确");
jf.setVisible(false);
//关闭数据库连接
Database.cn.close();
new Main();
}
else {
System.out.println("错误");
new JOptionPane().showMessageDialog(null,"用户名或密码错误!","",JOptionPane.ERROR_MESSAGE);//!!!!!!!!!!!!!!
}
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
else{
System.out.println("连接数据库不成功!!!");
}
}

public static void main(String args[]){
new Land();
}
}


需要主函数类的话我再上传
...全文
169 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjx320382 2016-03-26
  • 打赏
  • 举报
回复
//主函数类,可以独立运行 package classsource; import java.awt.*;//倒包 import java.awt.event.*;//倒包 import javax.swing.*;//倒包 import java.sql.*;//倒包 public class Main extends JFrame implements Runnable{ Thread t=new Thread(this);//在窗体里创建线程并实例化 JDesktopPane deskpane = new JDesktopPane();//在窗体里建立虚拟桌面并实例化 JPanel p = new JPanel();//创建一个面板并实例化 Label lp1=new Label("欢 迎 使 用 员 工 管 理 系 统 !"); //菜单上的图标创建并实例化---------------------------------------------------------------------------- ImageIcon icon1=new ImageIcon("image//tjsc.gif"); ImageIcon icon2=new ImageIcon("image//cxdl.gif"); ImageIcon icon3=new ImageIcon("image//xgmm.gif"); ImageIcon icon4=new ImageIcon("image//tcxt.gif"); ImageIcon icon5=new ImageIcon("image//jj.gif"); ImageIcon icon6=new ImageIcon("image//help.gif"); ImageIcon icon7=new ImageIcon("image//cx.gif"); ImageIcon icon8=new ImageIcon("image//gl.gif"); ImageIcon icon9=new ImageIcon("image//xt.gif"); ImageIcon icon10=new ImageIcon("image//xxgl.gif"); ImageIcon icon11=new ImageIcon("image//xxcx.gif"); ImageIcon icon12=new ImageIcon("image//bz.gif"); ImageIcon icon13=new ImageIcon("image//gy.gif"); ImageIcon icon14=new ImageIcon("image//glxx.gif"); ImageIcon icon15=new ImageIcon("image//cxxx.gif"); //完-------------------------------------------------------------------------------- public Main(){//构造函数 setTitle("员工管理系统");//设置窗体标题 Container con = getContentPane(); con.setLayout(new BorderLayout());//创建一个布局 con.add(deskpane,BorderLayout.CENTER);//实例虚拟桌面的布局 Font f =new Font("新宋体",Font.PLAIN,12);//设置一个字体,以后设置字体全部调用这种字体,懒得弄那么花花哨哨的 JMenuBar mb = new JMenuBar();//实例化菜单栏 //实例化菜单开始 JMenu systemM = new JMenu("系统管理"); systemM.setFont(f); JMenu manageM = new JMenu("信息管理"); manageM.setFont(f); JMenu employeeMM = new JMenu("员工信息管理");//这个是信息管理的二级菜单 employeeMM.setFont(f); JMenu selectM = new JMenu("信息查询"); selectM.setFont(f); JMenu employeeSM =new JMenu("员工信息查询");//这个是信息查询的二级菜单 employeeSM.setFont(f); //JMenu statisticM = new JMenu("休闲娱乐"); //statisticM.setFont(f); JMenu helpM = new JMenu("帮助"); helpM.setFont(f); JMenu aboutM=new JMenu("关于"); aboutM.setFont(f); //实例化菜单结束 //实例化系统管理菜单的菜单项 JMenuItem password = new JMenuItem("密码修改"); password.setFont(f); JMenuItem land = new JMenuItem("重新登陆"); land.setFont(f); JMenuItem addDelete = new JMenuItem("添加/删除用户"); addDelete.setFont(f); JMenuItem exit = new JMenuItem("退出系统"); exit.setFont(f); systemM.add(password); systemM.add(land); systemM.add(addDelete); systemM.add(exit); //实例化系统管理菜单的菜单项结束 //为系统管理菜单加事件----------------------------------------------------------------------------- password.addActionListener(new ActionListener(){//密码修改监听 public void actionPerformed(ActionEvent e){ System.out.println("AmendPassword"); deskpane.add(new AmendPassword()); } }); land.addActionListener(new ActionListener(){//重新登陆监听 public void actionPerformed(ActionEvent e){ System.out.println("Land"); setVisible(false); new Land(); } }); addDelete.addActionListener(new ActionListener(){//添加/删除用户监听 public void actionPerformed(ActionEvent e){ deskpane.add(new AddDeleteUser()); } }); exit.addActionListener(new ActionListener(){//退出系统监听 public void actionPerformed(ActionEvent e){ //new JOptionPane().showMessageDialog( setVisible(false); } }); //-------------------------------------------------------------------------------------------- //实例化信息管理的菜单项 JMenuItem departmentM = new JMenuItem("部门信息管理"); departmentM.setFont(f); JMenuItem employeeM = new JMenuItem("基本信息管理"); employeeM.setFont(f); JMenuItem trainM = new JMenuItem("培训信息管理"); trainM.setFont(f); JMenuItem encouragementPunishM = new JMenuItem("奖罚信息管理"); encouragementPunishM.setFont(f); JMenuItem wageM =new JMenuItem("薪资信息管理"); wageM.setFont(f); employeeMM.add(trainM); employeeMM.add(employeeM); employeeMM.add(encouragementPunishM); employeeMM.add(wageM); manageM.add(employeeMM); manageM.add(departmentM); //实例化信息管理的菜单项结束 //为管理菜单加事件------------------------------------------------------------------------------ departmentM.addActionListener(new ActionListener(){//部门信息管理监听 public void actionPerformed(ActionEvent e){ System.out.println("Departmentmanage"); deskpane.add(new Departmentmanage()); } }); employeeM.addActionListener(new ActionListener(){//基本信息管理监听 public void actionPerformed(ActionEvent e){ System.out.println("Employeemanage"); deskpane.add(new Employeemanage()); } }); trainM.addActionListener(new ActionListener(){//培训信息管理监听 public void actionPerformed(ActionEvent e){ System.out.println("Trainmanage"); deskpane.add(new Trainmanage()); } }); encouragementPunishM.addActionListener(new ActionListener(){//奖罚信息管理监听 public void actionPerformed(ActionEvent e){ System.out.println("EncouragementPunish"); deskpane.add(new EncouragementPunish()); } }); wageM.addActionListener(new ActionListener(){//薪资信息管理监听 public void actionPerformed(ActionEvent e){ System.out.println("WageManage"); deskpane.add(new WageManage()); } }); //----------------------------------------------------------------------------------------------- //实例化信息查询的菜单项 JMenuItem departmentS = new JMenuItem("部门信息查询"); departmentS.setFont(f); JMenuItem employeeS = new JMenuItem("基本信息查询"); employeeS.setFont(f); JMenuItem trainS = new JMenuItem("培训信息查询"); trainS.setFont(f); JMenuItem encouragementPunishS = new JMenuItem("奖罚信息查询"); encouragementPunishS.setFont(f); JMenuItem wageS =new JMenuItem("薪资信息查询"); wageS.setFont(f); employeeSM.add(trainS); employeeSM.add(employeeS); employeeSM.add(encouragementPunishS); employeeSM.add(wageS); selectM.add(employeeSM); selectM.add(departmentS); //实例化信息查询的菜单项结束 //为查询菜单加事件--------------------------------------------------------- departmentS.addActionListener(new ActionListener(){//部门信息查询监听 public void actionPerformed(ActionEvent e){ System.out.println("DIQ"); deskpane.add(new DIQ()); } }); employeeS.addActionListener(new ActionListener(){//基本信息查询监听 public void actionPerformed(ActionEvent e){ System.out.println("BIQ"); deskpane.add(new BIQ()); } }); trainS.addActionListener(new ActionListener(){//培训信息查询监听 public void actionPerformed(ActionEvent e){ System.out.println("TIQ"); deskpane.add(new TIQ()); } }); encouragementPunishS.addActionListener(new ActionListener(){//奖罚信息查询监听 public void actionPerformed(ActionEvent e){ System.out.println("EPIQ"); deskpane.add(new EPIQ()); } }); wageS.addActionListener(new ActionListener(){//薪资信息查询监听 public void actionPerformed(ActionEvent e){ System.out.println("SIQ"); deskpane.add(new SIQ()); } }); JMenuItem help = new JMenuItem("帮助"); help.setFont(f); JMenuItem about =new JMenuItem("关于"); about.setFont(f); helpM.add(help); aboutM.add(about); //为帮助菜单加事件------------------------------------------------------------------------- about.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ deskpane.add(new About()); } }); //----------------------------------------------------------------------------------------- mb.add(systemM); mb.add(manageM); mb.add(selectM); // mb.add(statisticM); mb.add(helpM); mb.add(aboutM); setJMenuBar(mb); //以下全都是在添加图标---------------------------------------------------------------------------------- //窗口图标 Image img=Toolkit.getDefaultToolkit().getImage("image\\main.gif"); setIconImage(img); //添加菜单图标 systemM.setIcon(icon9); manageM.setIcon(icon8); selectM.setIcon(icon7); // statisticM.setIcon(icon12); helpM.setIcon(icon6); addDelete.setIcon(icon1); land.setIcon(icon2); password.setIcon(icon3); exit.setIcon(icon4); employeeMM.setIcon(icon5); employeeSM.setIcon(icon5); departmentM.setIcon(icon10); departmentS.setIcon(icon11); // game.setIcon(icon12); // countStatistic.setIcon(icon12); help.setIcon(icon6); aboutM.setIcon(icon13); about.setIcon(icon13); employeeM.setIcon(icon14); trainM.setIcon(icon14); encouragementPunishM.setIcon(icon14); wageM.setIcon(icon14); employeeS.setIcon(icon15); trainS.setIcon(icon15); encouragementPunishS.setIcon(icon15); wageS.setIcon(icon15); //添加完了----------------------------------------------------------------------------- 还有下半部分分下页传
mjx320382 2016-03-26
  • 打赏
  • 举报
回复
可以加我QQ私聊 2-7-9-4-5-2-发(8)-3-5-0

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧