我设置并连接了一个名为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文件,所以我没有包含它们)我收到错误消息说有没有架构&#34;密码&#34;。我真的很困惑这里发生的事情,因为这不是我以前做过的事情,而是我试图自己解决这个问题。我认为可能找不到桌子。我使用的是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
答案 0 :(得分:0)
以下两种基本技巧可以获取有关您的程序如何访问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