Java 创建了表,但它在哪里?
我设置并连接了一个名为Java 创建了表,但它在哪里?,java,sql,derby,glassfish-4,eclipse-kepler,Java,Sql,Derby,Glassfish 4,Eclipse Kepler,我设置并连接了一个名为timezonedb 然后我使用SQL剪贴簿创建表Users。 如您所见,该表已通过剪贴簿成功创建、添加和查询。问题是,它似乎不在timezonedb中列出的任何模式中。当我试图运行我的程序时,它似乎认为密码是一个模式。涉及的Java代码如下: package cis407; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; impo
timezonedb
然后我使用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);
}
}
以下是两种基本技术,用于获取有关程序如何访问Derby以及出错原因的更多信息: 首先,在启用Derby.language.logStatementText参数的情况下运行Derby,并学习读取您的Derby.log文件:
其次,通过使用以下技术从SQL异常中获取更多信息:您得到的确切错误是什么?也许可以尝试在用于管理数据库本身的程序中执行查询(使用填充的
?
),并查看是否仍然出现错误。这可能是一个简单的命名问题。添加到帖子中,我不知道你说的“尝试并使用填充的?”执行查询是什么意思。你是说在SQL剪贴簿中尝试这样做吗?他的意思是尝试使用rawQuery执行一个文字SQL语句,看看你准备的语句是否有问题,或者数据库创建或访问是否真的有问题。不幸的是,自从我发布这篇文章以来,我一直试着让它工作,但不知怎么的,我把它弄坏了,现在连连接都无法连接。
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