我是Java Server Faces和JavaDB的新手。由于错误声明“Schema'ROOT'不存在”,我正在努力连接由于我的数据库。我把头发拉过来,我知道它一定很简单。我没有为数据库“guest_book”设置用户名或密码。数据库存在于APP模式下。托管bean编码如下..
package com.jsf;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
/**
*
* @author pctdeveloper7
*/
@ManagedBean(name="guestbean")
@SessionScoped
public class GuestBookBean {
private String date;
private String fname;
private String lname;
private String email;
private String message;
@Resource( name="jdbc/guest_book" )
DataSource ds;
/**
* Creates a new instance of NewJSFManagedBean
*/
public GuestBookBean() {
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
//return a resultset of entries
public ResultSet getEntries() throws SQLException{
//Check if was injected by the server
if(ds == null)
throw new SQLException("Unable to obtain datsource");
//get connection from datapool
Connection conn = ds.getConnection("root","root");
//check if connection was successful
if(conn==null)
throw new SQLException("Unable to connect to DataSource");
try{
//Create a prepared statement to insert a new address book entry
PreparedStatement getMessages = conn.prepareStatement("Select * " +
"FROM MESSAGES ORDER BY lname, fname");
CachedRowSet rowset= new com.sun.rowset.CachedRowSetImpl();
rowset.populate(getMessages.executeQuery());
return rowset;
} finally{
conn.close(); //return connection to pool
}
}//End getEntries
//Save a new guestbook message
public String save() throws SQLException{
//Check if was injected by the server
if(ds == null)
throw new SQLException("Unable to obtain datsource");
//get connection from datapool
Connection conn = ds.getConnection();
//check if connection was successful
if(conn==null)
throw new SQLException("Unable to connect to DataSource");
try{
//create a preparedStatement to insert a new guestbook entry
PreparedStatement insertEntry = conn.prepareStatement("INSERT INTO"+
"messages values( ?, ?, ?, ?, ?)");
//define prepared statements arguements
insertEntry.setString(1, fname);
insertEntry.setString(2, lname);
insertEntry.setString(3, email);
insertEntry.setString(4, message);
insertEntry.setString(5, date);
insertEntry.executeUpdate();// insert the new entry
return "index"; //go back to the index page
}finally{
conn.close();//return connection to the pool
}
}//END Save()
}
用于创建数据库的SQL如下..
create table messages
(
fname varchar(25),
lname varchar(35),
email varchar(50),
message varchar(300),
"DATE" varchar(11)
);
Insert into messages
values('Jared', 'Rainey', 'jared.rainey.it@gmail.com', 'Hi!', '10-12-1982');
连接池名为GuestBookPool,数据源为jdbc / guest_book。我没有为任何东西设置密码或用户名,它都是根据我的理解在APP架构下创建的。任何帮助将不胜感激!
答案 0 :(得分:0)
而不是@ManagedBean(name="guestbean");
尝试@ManagedBean(name="guestbean", schema="APP");
或您的架构。
答案 1 :(得分:-1)
如果您使用的是JSF,则可以通过在项目中显示的Web-INF文件夹中创建ApplicationContext来连接数据库。
创建所需数据源的单例bean。
您使用的方法不是首选方法。
顺便说一句,为了帮助您,我可以问您正在使用哪个数据库? Oracle,postgres,Access等?