我曾尝试设置 derby 和 mysql 数据库,但两次都找不到表。我已经创建了表,并从网络上的不同设备连接到它。我只是无法使用连接池连接到它。
对于另一个示例,我收到以下消息“java.sql.SQLSyntaxErrorException: Table/View 'USERS' 不存在”或“Table/View 'ADDRESSES' 不存在”。
我可以 ping 数据库,使用驱动程序管理器一切正常,但使用连接池无法正常工作。我的池配置:
user:user
password:password
url:jdbc:mysql://localhost:3306/thechef
port:3306
DatabaseName:thechef
ServerName:localhost
这是我的文件:
用户.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;
@ManagedBean
@SessionScoped
public class Users {
private String userName;
private String firstName;
private String lastName;
private String city;
private String zipcode;
private String state;
private String country;
private String email;
@Resource (name="jdbc/thechef")
DataSource ds;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String save() throws SQLException
{
if (ds==null)
throw new SQLException ("unable to obtain datasource");
Connection connection = ds.getConnection();
if (connection==null)
throw new SQLException ("unable to obtain datasource");
try{
PreparedStatement addEntry = connection.prepareStatement("INSERT INTO USERS (USERNAME, FIRSTNAME, LASTNAME)VALUES (?,?,?)");
addEntry.setString(1, getUserName());
addEntry.setString(2, getFirstName());
addEntry.setString(3, getLastName());
addEntry.executeUpdate();
return "index";
}
finally{
connection.close();
}
}
}