这是错误:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at SqlUtil.acquireConnection(SqlUtil.java:34)
at MainApp.connectToDB(MainApp.java:89)
at MainApp.<init>(MainApp.java:68)
at MainApp.main(MainApp.java:206)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MainApp.hideSplashScreen(MainApp.java:128)
at MainApp$3.run(MainApp.java:77)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
主界面程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
class MainApp extends JFrame
{
//系统启动时的等待画面
private JFrame splashScreen=null;
//管理菜单
private JMenu MMenu=new JMenu("管理");
private JMenuItem StudentResultM=new JMenuItem("学生成绩管理");
//设置菜单
private JMenu SetMenu=new JMenu("设置");
private JMenuItem SysConfig=new JMenuItem("系统参数设置");
//帮助菜单
private JMenu HelpMenu=new JMenu("帮助");
JMenuItem aboutMe=new JMenuItem("关于本程序");
//内容窗格
private Container contentPane;
//当前内容窗格中的面板
private JPanel selectedPanel;
//状态信息
JLabel labStatusContent;
//登录用户
private JLabel labUserContent;
//数据库连接
Connection con=null;
//参数信息
private Properties prop=null;
public MainApp()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
showSplashScreen();
}
});
try
{
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.print(e);
}
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
hideSplashScreen();
}
});
this.setTitle("学生成绩管理系统");
contentPane=this.getContentPane();
//创建菜单
constructMenu();
//创建状态条
constructStatusPanel();
//读入系统参数
prop=SqlUtil.loadProperty("config/config.properties");
//建立数据库连接
connectToDB();
//添加导航面板
if(con!=null)
setSelectedPanel(new NavigatorPanel(this));
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
hideSplashScreen();
}
});
}
//连接到数据库
public void connectToDB()
{
try{
con=SqlUtil.acquireConnection(prop.getProperty("DB_IP"),
prop.getProperty("DB_PORT"),
prop.getProperty("DB_NAME"),
prop.getProperty("DB_USER_NAME"),
prop.getProperty("DB_USER_PWD"));
}
catch(Exception e)
{
con=null;
this.labStatusContent.setText("不能连接到数据库!"+
"请修改系统设置后重新启动该程序. "
+e.toString());
return;
}
}
public void showSplashScreen()
{
//取得屏幕大小
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
ImageIcon image=null;
try
{
image=new ImageIcon("image/splash.jpg");
}
catch(Exception e)
{
System.out.println(e);
}
JLabel splashLabel = new JLabel(image);
splashScreen = new JFrame();
//去掉JFrame的装饰边框
splashScreen.setUndecorated(true);
splashScreen.getContentPane().add(splashLabel);
splashScreen.pack();
int width=image.getImage().getWidth(null);
int height=image.getImage().getHeight(null);
//快闪屏幕居中
splashScreen.setLocation(new Point(((int)d.getWidth()-width)/2,
((int)d.getHeight()-height)/2));
splashScreen.setVisible(true);
}
public void hideSplashScreen()
{
splashScreen.setVisible(false);
splashScreen=null;
}
public void constructMenu()
{
//构建菜单
JMenuBar menuBar=new JMenuBar();
StudentResultM.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MainApp.this.setSelectedPanel(new DBSFPanel(MainApp.this,con));
}
});
MMenu.add(StudentResultM);
menuBar.add(MMenu);
SysConfig.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new ConfigDialog(MainApp.this).setVisible(true);
}
});
SetMenu.add(SysConfig);
menuBar.add(SetMenu);
aboutMe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new About().setVisible(true);
}
});
HelpMenu.add(aboutMe);
menuBar.add(HelpMenu);
setJMenuBar(menuBar);
}
//创建状态条
public void constructStatusPanel()
{
JPanel statusPanel=new JPanel();
Border loweredBevelBorder=BorderFactory.createLoweredBevelBorder();
statusPanel.setLayout(new GridBagLayout());
JLabel labStatus=new JLabel(" 提示信息 ");
labStatusContent=new JLabel();
JLabel labUser=new JLabel(" 用户名 ");
labUserContent=new JLabel();
labUserContent.setPreferredSize(new Dimension(90,3));
labUser.setBorder(loweredBevelBorder);
labUserContent.setBorder(loweredBevelBorder);
labStatus.setBorder(loweredBevelBorder);
labStatusContent.setBorder(loweredBevelBorder);
LayoutUtil.add(statusPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,0,1,1,labStatus);
LayoutUtil.add(statusPanel,GridBagConstraints.BOTH,
GridBagConstraints.CENTER,100,100,1,0,1,1,labStatusContent);
LayoutUtil.add(statusPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,labUser);
LayoutUtil.add(statusPanel,GridBagConstraints.VERTICAL,
GridBagConstraints.CENTER,0,100,3,0,1,1,labUserContent);
contentPane.add(statusPanel,BorderLayout.SOUTH);
}
public void setSelectedPanel(JPanel p)
{
if(selectedPanel!=null)
contentPane.remove(selectedPanel);
selectedPanel=p;
contentPane.add(selectedPanel,"Center");
this.validate();
}
public static void main(String []args)
{
MainApp ma=new MainApp();
ma.setDefaultCloseOperation(EXIT_ON_CLOSE);
ma.setSize(600,400); //设置主画面初始大小
ma.setVisible(true);
}
}