当前位置: 技术问答>java相关
jdbc使用错误,请高手帮忙。
来源: 互联网 发布时间:2017-03-19
本文导语: 我在test项目中想用jdbc方法连接oracle数据库,驱动已经设置好,在tool-database depilot中已经能够执行查询语句,但在程序报很多错。项目中有application1.java和Frame1.java,都是按向导生成的,在Frame1.java中加了一个菜单项,...
我在test项目中想用jdbc方法连接oracle数据库,驱动已经设置好,在tool-database depilot中已经能够执行查询语句,但在程序报很多错。项目中有application1.java和Frame1.java,都是按向导生成的,在Frame1.java中加了一个菜单项,下面是Frame1.java的代码。lass.forName .newInstance() DriverManager.getConnection(url,user,password) conn.createStatement() stmt.executeQuery(sql) while(rs.next()) rs.getString(1) rs.close();
stmt.close(); conn.close(); 等处都报错“Unreported exception:java.lang.ClassNotFoundException;must be caught or declared to be thrown at line 95”.应该怎样修改?
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class Frame1 extends JFrame {
private JPanel contentPane;
private JMenuBar jMenuBar1 = new JMenuBar();
private JMenu jMenuFile = new JMenu();
private JMenuItem jMenuFileExit = new JMenuItem();
private JMenu jMenuHelp = new JMenu();
private JMenuItem jMenuHelpAbout = new JMenuItem();
private BorderLayout borderLayout1 = new BorderLayout();
private JMenuItem jMenuFileJdbc = new JMenuItem();
private Connection conn;
private Statement stmt;
private ResultSet rs;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuHelpAbout_actionPerformed(e);
}
});
jMenuFileJdbc.setText("jdbc");
jMenuFileJdbc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileJdbc_actionPerformed(e);
}
});
jMenuFile.add(jMenuFileExit);
jMenuFile.add(jMenuFileJdbc);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
void jMenuFileJdbc_actionPerformed(ActionEvent e) {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="oracle:thin:@orc1.houji.com:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
String sql="select * from scott.dept";
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
};
rs.close();
stmt.close();
conn.close();
}
}
stmt.close(); conn.close(); 等处都报错“Unreported exception:java.lang.ClassNotFoundException;must be caught or declared to be thrown at line 95”.应该怎样修改?
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class Frame1 extends JFrame {
private JPanel contentPane;
private JMenuBar jMenuBar1 = new JMenuBar();
private JMenu jMenuFile = new JMenu();
private JMenuItem jMenuFileExit = new JMenuItem();
private JMenu jMenuHelp = new JMenu();
private JMenuItem jMenuHelpAbout = new JMenuItem();
private BorderLayout borderLayout1 = new BorderLayout();
private JMenuItem jMenuFileJdbc = new JMenuItem();
private Connection conn;
private Statement stmt;
private ResultSet rs;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuHelpAbout_actionPerformed(e);
}
});
jMenuFileJdbc.setText("jdbc");
jMenuFileJdbc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuFileJdbc_actionPerformed(e);
}
});
jMenuFile.add(jMenuFileExit);
jMenuFile.add(jMenuFileJdbc);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
void jMenuFileJdbc_actionPerformed(ActionEvent e) {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="oracle:thin:@orc1.houji.com:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
String sql="select * from scott.dept";
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
};
rs.close();
stmt.close();
conn.close();
}
}
|
Unreported exception:java.lang.ClassNotFoundException;must be caught or declared to be thrown at line 95”
这个错误,你看一下,就是说那句话要抛出异常,要你捕捉
try{
//有异常的语句比如stmt.close();
}catch(Exception ex){
//异常处理语句
}
这个错误,你看一下,就是说那句话要抛出异常,要你捕捉
try{
//有异常的语句比如stmt.close();
}catch(Exception ex){
//异常处理语句
}
|
这里下载驱动
http://www.52jsp.com/resource/
http://www.52jsp.com/resource/
|
出错的是这句:
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
没有找到合适的jdbc的api,确定你的classpath里加入了该jdbc的路径吗?
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
没有找到合适的jdbc的api,确定你的classpath里加入了该jdbc的路径吗?