无法在Web Logic derby数据源上执行语句

时间:2019-06-21 01:42:59

标签: java-ee weblogic derby

我正在使用WebLogic Server访问数据库。我已通过管理控制台将数据库添加到WebLogic Server实例中。这是一个Apache derby DB。我可以在代码中建立连接,但是每次尝试执行语句时,都会出现异常:

java.sql.SQLSyntaxErrorException:模式'WEBLOGIC'不存在         在org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(未知来源)         在org.apache.derby.impl.jdbc.Util.generateCsSQLException(来源不明)         在org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(未知来源)

与他人交谈时,我听说也许我的derby版本较旧,但我只是下载了最新版本(10.14.2.0)。我还注意到WLS拥有自己的derby目录(Oracle_Home \ wlserver \ common \ derby),因此我尝试用最新版本替换lib目录,但这无济于事。

当我尝试执行语句时,我的InsertStudents方法中发生了异常。我在这里完全迷路了。

  public class JNDIPrinter {
  private static String tableName = "student";
  // jdbc Connection
  private static Connection conn = null;
  private static Statement stmt = null;

  public void AddStudents() {
    Context ctx = null;
    try {
      conn = GetConnection();
      List<Student> students;

      students = new ArrayList<Student>(
          Arrays.asList(new Student("first1", "last1", "111-11-1111", "1@1.com", "111 1st st", "user1", "pw1"),
              new Student("first2", "last2", "222-22-2222", "2@2.com", "222 2nd st", "user2", "pw2"),
              new Student("first3", "last3", "333-33-3333", "3@3.com", "333 3rd st", "user3", "pw3")));

      InsertStudents(students);
    } catch (Exception e) {
      System.out.println(e.toString());
    } finally {
      if (ctx != null) {
        try {
          ctx.close();
        } catch (NamingException e) {
          System.out.println("Failed to close context due to: " + e);
        }
      }
    }
  }

  private Connection GetConnection() throws NamingException, SQLException {
    Context ctx = null;
    String url = null;
    System.out.println(url);
    Hashtable env = new Hashtable();
    // This *required* property specifies the factory to be used
    // to create the context.
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

    if (url != null) {
      // This property specifies the URL of the WebLogic Server that will
      // provide the naming service. Defaults to t3://localhost:7001
      env.put(Context.PROVIDER_URL, url);
    }

    ctx = new InitialContext(env);
    System.out.println("Initial context created");
    DataSource ds = (javax.sql.DataSource) ctx.lookup("jhuDataSource");
    System.out.println("Datasource created");
    java.sql.Connection connection = (java.sql.Connection) ds.getConnection();
    return connection;
  }

  private void InsertStudents(List<Student> students) throws SQLException{
    try {
      for (Student student : students) {
        stmt = conn.createStatement();
        stmt.execute("insert into " + tableName + " values ('" 
                                                  + student.first_name + "','" 
                                                  + student.last_name +  "','" 
                                                  + student.ssn +       "','"
                                                  + student.email +     "','" 
                                                  + student.address +   "','" 
                                                  + student.userID +    "','" 
                                                  + student.password +  "')");
        stmt.close();
      }
    } catch (SQLException sqlExcept) {
      sqlExcept.printStackTrace();
    }
  }
}
```````

0 个答案:

没有答案