bluej,连接到mysql而不是derby

时间:2018-06-15 20:45:47

标签: java connection derby

我有以下项目。我在mysql中加载了相同的数据库,也加载了derby。我能够连接到mysql但是当我尝试连接到derby时收到以下错误消息:

package db;

import java.lang.reflect.Method;
import tables.students;
import tables.teacher;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;


public class DBSelect {
    static String driver;
    static String database;
    static String table;


    public static void main(String[] args) throws SQLException  {
        table = "students";
       try(Connection con = DBConnection.getConnection(driver="derby",database="school");
             Statement stmt = 
                con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
                ResultSet.CONCUR_UPDATABLE);
             ResultSet rs1 = stmt.executeQuery("select * from " + table);
            )
        {  
            students.getStudents(rs1);
            ResultSet rs2 = stmt.executeQuery("select * from teacher");
            System.out.println("");
            teacher.getTeacher(rs2);

            rs2.first();

            try
            {rs2.getInt("fee");
              System.out.println("found");
              stmt.executeUpdate("alter table teacher drop fee  ");
            }catch(SQLException e)
            {
                System.out.println("not found");
                System.err.print(e);
                stmt.executeUpdate("alter table teacher add fee int ");
            } 


        } catch (SQLException e) {
            System.err.print(e);

        }  
    }    
}

这是代码:

dbconnect.java:

Set

dbselect.java:

Set

1 个答案:

答案 0 :(得分:1)

假设derby数据库存在(如果不修改jdbc url以在第一次运行中包含'create'选项:jdbc:derby:derbyDB;create=true

- 从syserr更改为printstacktrace以获取更多信息:

    } catch (SQLException e) {
        e.printStackTrace();
    }

最简单的情况:你已经运行了一个锁定了数据库的java实例,

奇怪的情况:缺少'log'目录

最坏情况:数据库已损坏。