Java 无法访问目标,标识符';数据库操作&x27;已解析为空
你好,我试着做一个db操作 这是我的豆子Java 无法访问目标,标识符';数据库操作&x27;已解析为空,java,Java,你好,我试着做一个db操作 这是我的豆子 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ // Import Statements import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLEx
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
// Import Statements
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.ManagedBean;
@ManagedBean
public class DatabaseOperation {
static final String DbDriver = "com.mysql.jdbc.Driver"; // To set the driver
static final String strConnection = "jdbc:mysql://localhost:3306/TekirMobile"; // Connection string
static final String strDbUsername = "root"; // Username of the database user
static final String strDbPassword = "fenderpass"; // Password of the database user
static Connection DatabaseConnection; // Connection object to establish the connection to the database
static ResultSet RsStudent; // Resultset to store the information retrieved from the database
static Statement DatabaseStatement; // Statement object of the database connection
String beginDate;
public DatabaseOperation() // Constructor of the class
{
makeConnection(); // To establish the database connection
}
public void makeConnection() // To establish the database connection, No return value and parameters
{
try {
Class.forName(DbDriver); // Setting the driver
DatabaseConnection = DriverManager.getConnection(strConnection, strDbUsername, strDbPassword); // Establishing the connection with the database
DatabaseStatement = DatabaseConnection.createStatement(); // Assigning the statement to the connection
System.out.println("Connection Success....");
} catch (Exception e) // In case of any Exception
{
System.out.println(e); // Print the Exception
}
}
public ResultSet selectFromDatabase(String strSelectQuery) // A function which retrieves Records from the database, Returns Recordset, Query as parameter
{
try {
RsStudent = DatabaseStatement.executeQuery(strSelectQuery); // Execute the select query
return RsStudent; // Returns the resultset
} catch (Exception e) {
System.out.println(e); // Print the exception
return null; // Return NULL
}
}
public String submit() {
System.out.println("Submitted value: " + beginDate);
return null;
}
public void disConnect() {
try {
DatabaseConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}
这是我的jsf
<h:panelGroup>
<h:outputLabel id="beginDate" value="Begin Date:" /><br />
<h:inputText value="#{DatabaseOperation.beginDate}" style="width:95%;" />
</h:panelGroup>
这也是我的按钮
<h:panelGroup>
<h:commandButton type="submit" action="#{DatabaseOperation.submit}" />
</h:panelGroup>
当我按下按钮,我得到这个错误为什么?怎么了?我需要做什么?我的豆子对吗?我是否向bean发送了值?我认为当您尝试从JSF页面访问bean时,第一个字母应该较低:
<h:panelGroup>
<h:commandButton type="submit" action="#{databaseOperation.submit}" />
</h:panelGroup>
试着像上面那样设置名称。当您尝试像上面那样设置名称时会发生什么?在服务器启动期间,日志是否显示了什么?显然,这个bean无法解析。我需要把我的豆子添加到某个地方吗?在任何地方都是.xml?这应该不是必需的,但也许您的项目在某种程度上设置错误。你还有其他的bean可以工作吗?那么我建议看一个JSF教程,用bean创建一个简单的项目。
@ManagedBean(name="databaseOperation")