我是 Java Server Faces 和 JavaDB 的新手。由于“架构'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 模式下创建的。任何帮助将不胜感激!