关于数据库查询的问题!
我现在想在程序启动时,在一个主类里头直接完成jdbc-odbc桥接,且只连接数据库一次
然后其他类中当要用到具体查询的时候,就另外生成 ResultSet rs
可是现在确老是报这个错误:Hit uncaught exception java.lang.NullPointerException
请指教...
问题点数:80、回复次数:8Top
1 楼lydvqq(碧水情缘♀黑哥)回复于 2006-03-03 21:18:42 得分 0
应是哪个变量没被实例化.而用了该变量.
最好贴源码.Top
2 楼watermelon01(奔跑,然后再奔跑)回复于 2006-03-03 21:25:41 得分 0
//***此处是主类
public class MainApp {
boolean packFrame = false;
String url="jdbc:odbc:kqsys";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con;
//Construct the application
public MainApp() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
//*******************
//connect the database
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"sa","sa");
}
catch(Exception e){
System.out.println(" "+e);
}
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MainApp();
}
}
Top
3 楼watermelon01(奔跑,然后再奔跑)回复于 2006-03-03 21:31:48 得分 0
//此类中需要查询
public class MainFrame extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
JButton jButton4 = new JButton();
MainApp mp1;
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
............
//**此处开始使用..
void jButton4_actionPerformed(ActionEvent e) {
if(e.getSource()==jButton4){
String sql="SELECT * FROM kqemp";
try {
Statement stmt=mp1.con.createStatement();
ResultSet rs=stmt.executeQuery("select * from kqemp");
while(rs.next()){
String ename=rs.getString(3);
System.out.println(" "+ename);}
rs.close();
}
catch(SQLException ee){System.out.println(" "+ee);}
}
}
}
.........Top
4 楼lydvqq(碧水情缘♀黑哥)回复于 2006-03-03 21:46:41 得分 60
最好在MainApp中定义个public getConnection方法来取con.这样才合javar的习惯.因为你MainApp的con只有在它的子类或本身可以用.
以下错误:
Statement stmt=mp1.con.createStatement();Top
5 楼lydvqq(碧水情缘♀黑哥)回复于 2006-03-03 21:51:39 得分 0
上面写快了.有点错
public Connection getConnection()来取con
Statement stmt=mp1.getConnection().createStatement();
Top
6 楼zx2002027(http://www.netyi.net/in.asp?id=zx2002027)回复于 2006-03-04 13:09:22 得分 10
MainApp mp1;没有实例化
正如楼上所言,最好写个函数来返回 Connection 对象Top
7 楼xiangzhengyan(千里单骑走西藏)回复于 2006-03-04 13:48:01 得分 0
upTop
8 楼f_acme(沧海一声笑)回复于 2006-03-04 15:37:59 得分 10
Hit uncaught exception java.lang.NullPointerException
看错误应该是你在某个可能发生异常的地方没有去捕获异常。Top




