【问题标题】:Created table, but where is it?创建表,但它在哪里?
【发布时间】:2013-11-24 00:14:17
【问题描述】:

我设置并连接了一个名为 timezonedb 的 derby 数据库 然后我使用 SQL 剪贴簿创建表Users

如您所见,表格已成功创建、添加到剪贴簿并通过剪贴簿查询。问题是,它似乎不在 timezonedb 中列出的任何模式中。当我尝试运行我的程序时,似乎认为密码是一种模式。涉及的Java代码如下:

    package cis407;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
   This bean formats the local time of day for a given date
   and city.
*/
@ManagedBean
@SessionScoped
public class TimeZoneBean
{
@Resource(name="jdbc/__default")
private DataSource source;

private String userName;
private String password;
private DateFormat timeFormatter;
private String city;
private TimeZone zone;
private String errorMessage;

/**
  Initializes the formatter.
 */
public TimeZoneBean()
{
    timeFormatter = DateFormat.getTimeInstance();
}

public String getPassword()
{
    return password;
}
/**
 * setter for password property
 * there is no getter for the password because there is no reason to ever
 * return the password
 * @param password the password for a given user
 */
public void setPassword(String pass)
{
    password = pass;
}

/**
 * getter for applicable error message
 * @return the error message
 */
public String getErrorMessage()
{
    return errorMessage;
}

/**
 * Setter for username property
 * @param name the user who is logged in
 */
public void setUserName(String name)
{
    userName = name;
}

/**
 * getter for username property
 * @return the user who is logged in
 */
public String getUserName()
{
    return userName;
}

/**
  Setter for city property.
  @param aCity the city for which to report the local time
 */
public void setCity(String aCity)
{      
    city = aCity;
}

/**
  Getter for city property.
  @return the city for which to report the local time
 */
public String getCity()
{
    return city;
}

/**
  Read-only time property.
  @return the formatted time
 */
public String getTime()
{
    if (zone == null) return "not available";
    timeFormatter.setTimeZone(zone);
    Date time = new Date();
    String timeString = timeFormatter.format(time);
    return timeString;
}

/**
  Action for checking a city.
  @return "next" if time zone information is available for the city,
  "error" otherwise
 * @throws SQLException 
 */
public String checkCity() throws SQLException
{
    zone = getTimeZone(city);      
    if (zone == null) return "error";
    addCity();
    return "next";
}

public String newUser() throws SQLException
{
    if (source == null) 
      {
         Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(null, "No database connection");;
         return null;
      }
      Connection conn = source.getConnection();
      try
      {
         PreparedStatement stat = conn.prepareStatement(
                 "SELECT UserName "
                 + "FROM Users "
                 + "WHERE UserName=?");
         stat.setString(1, userName);
         ResultSet result = stat.executeQuery();
         if (result.next()) 
         {
            errorMessage = "There is already a user with that name. Please choose another.";
            return "login";
         }
         else
         {
            errorMessage = "";
            stat = conn.prepareStatement(
                    "INSERT INTO Users"
                    + "VALUES (?, ?, ?)");
            stat.setString(1, userName);
            stat.setString(2, password);
            stat.setString(3, null);
            stat.executeUpdate();
            return "index";
         }
      }
      finally
      {
         conn.close();
      }
}

public String logIn() throws SQLException
{
    if (source == null) 
      {
         Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(null, "No database connection");;
         return null;
      }
      Connection conn = source.getConnection();
      try
      {
         PreparedStatement stat = conn.prepareStatement(
            "SELECT FavCity FROM Users WHERE UserName=? AND Password=?");
         stat.setString(1, userName);
         stat.setString(2, password);
         ResultSet result = stat.executeQuery();
         if (result.next()) 
         {
            city = result.getString("FavCity");
            errorMessage = "";
            return "next";
         }
         else
         {
            errorMessage = "Wrong username or password";
            return "login";
         }
      }
      finally
      {
         conn.close();
      }
}

private void addCity() throws SQLException
{
    if (source == null) 
      {
         Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(null, "No database connection");;
         return;
      }
      Connection conn = source.getConnection();
      try
      {
         PreparedStatement stat = conn.prepareStatement(
            "UPDATE Users "
            + "SET FavCity=? "
            + "WHERE UserName=?");
         stat.setString(1, city);
         stat.setString(2, userName);
         stat.executeUpdate();
      }
      finally
      {
         conn.close();
      }
}
/**
  Looks up the time zone for a city.
  @param aCity the city for which to find the time zone
  @return the time zone or null if no match is found
 */
private static TimeZone getTimeZone(String aCity)
{
    String[] ids = TimeZone.getAvailableIDs();
    for (int i = 0; i < ids.length; i++)
        if (timeZoneIDmatch(ids[i], aCity))
            return TimeZone.getTimeZone(ids[i]);
    return null;
}

/**
  Checks whether a time zone ID matches a city.
  @param id the time zone ID (e.g. "America/Los_Angeles")
  @param aCity the city to match (e.g. "Los Angeles")
  @return true if the ID and city match
 */
private static boolean timeZoneIDmatch(String id, String aCity)
{
    String idCity = id.substring(id.indexOf('/') + 1);
    return idCity.replace('_', ' ').equals(aCity);
}
}

通过单击 webapp 中的按钮执行 logIn() 时(有一些 xhtml 文件我很确定不是问题,所以我没有包含它们)我收到一条错误消息,指出没有架构“密码”。我真的很困惑这里发生了什么,因为这不是我以前做过的事情,我正试图自己弄清楚。我认为它可能找不到表。我正在使用 GlassFish,我一直在搞乱其管理控制台中的 JDBC 设置,但是有太多东西我很难弄清楚问题出在哪里或出在哪里。

确切的错误是:

type Exception report
messageInternal Server Error
description The server encountered an internal error that prevented it from fulfilling this request.

exception
javax.servlet.ServletException: java.sql.SQLSyntaxErrorException: Schema 'PASSWORD' does not exist
root cause

javax.faces.el.EvaluationException: java.sql.SQLSyntaxErrorException: Schema 'PASSWORD' does not exist
root cause

java.sql.SQLSyntaxErrorException: Schema 'PASSWORD' does not exist
root cause

org.apache.derby.client.am.SqlException: Schema 'PASSWORD' does not exist

【问题讨论】:

  • 您遇到的 exact 错误是什么?也许尝试在您用于管理数据库本身的程序中执行查询(填写?'s),看看您是否仍然收到错误。这可能是一个简单的命名问题。
  • 已添加到帖子中,我不确定您的意思是尝试执行带有 ? 的查询。您是说在 SQL 剪贴簿中尝试这样做吗?
  • 他的意思是尝试使用 rawQuery 执行一个文字 SQL 语句,看看你准备好的语句是否有问题,或者数据库创建或访问是否真的有问题。

标签: java sql derby glassfish-4 eclipse-kepler


【解决方案1】:

这里有两种基本技术,可用于获取有关您的程序如何访问 Derby 以及出现什么问题的更多信息:

首先,在启用 derby.language.logStatementText 参数的情况下运行 Derby,并学习阅读您​​的 derby.log 文件:http://db.apache.org/derby/docs/10.10/ref/rrefproper43517.html

其次,使用以下技术从 SQL 异常中获取更多信息:http://wiki.apache.org/db-derby/UnwindExceptionChain

【讨论】:

  • 不幸的是,自从我发布这篇文章以来,我一直在努力让它工作,但不知何故我把它弄得更糟了,现在它甚至无法连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多