连接数据库时出错

时间:2011-03-31 07:32:50

标签: java database

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jjavadb;
import java.awt.Container;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
 *
 * @author ritesh
 */
public class Main extends JFrame{


    public Main() throws SQLException
    {super("Database of Students");
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        }
     Connection connection = DriverManager.getConnection( "jdbc:derby://localhost:1527/Jaane","hello","Hello" );
     Statement statement = connection.createStatement();
     ResultSet resultSet =
 statement.executeQuery( "SELECT * FROM COLLEAGUES" );
       StringBuffer results = new StringBuffer();
 ResultSetMetaData metaData = resultSet.getMetaData();
 int numberOfColumns = metaData.getColumnCount();

 for ( int i = 1; i <= numberOfColumns; i++ ) {
 results.append( metaData.getColumnName( i ) + "\t" );
 }

 results.append( "\n" );

 while ( resultSet.next() ) {

 for ( int i = 1; i <= numberOfColumns; i++ ) {
 results.append( resultSet.getObject( i )  );
results.append("\t"); }


 }

 // close statement and connection
 statement.close();
 connection.close();

// set up GUI and display window
JTextArea textArea = new JTextArea( "Hello");
 Container container = getContentPane();

 container.add( new JScrollPane( textArea ) );

 setSize( 300, 100 ); // set window size
 setVisible( true );
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         Main window = null;
        try {
            window = new Main();
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

我收到此错误

Mar 31, 2011 12:42:37 PM jjavadb.Main <init>
SEVERE: null
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at jjavadb.Main.<init>(Main.java:29)
        at jjavadb.Main.main(Main.java:78)
Mar 31, 2011 12:42:37 PM jjavadb.Main main
SEVERE: null
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/sample
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at jjavadb.Main.<init>(Main.java:34)
        at jjavadb.Main.main(Main.java:78)
Exception in thread "main" java.lang.NullPointerException
        at jjavadb.Main.main(Main.java:82)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

请一些帮助删除此错误我创建了带有用户名hello和密码的数据库你好请删除错误。请帮助.............

2 个答案:

答案 0 :(得分:1)

错误信息非常清楚 - Java无法找到数据库连接的jar文件(假设

Class.forName("org.apache.derby.jdbc.ClientDriver"); 

是发生错误的行)。你的CLASSPATH中有derby jar文件吗?

如果这没有帮助,你也可以看看
http://old.nabble.com/Activity-4-Error:-No-suitable-driver-found...-td15946730.html

答案 1 :(得分:1)

java.lang.ClassNotFoundException:org.apache.derby.jdbc.ClientDriver告诉您derby库不在类路径中。 您需要在类路径中包含derby库才能使其正常工作。