目标无法访问,标识符“DatabaseOperation”已解析为null

时间:2013-03-24 14:08:10

标签: java

您好我尝试进行数据库操作。

这是我的豆子

/*
 * 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的价值吗?

1 个答案:

答案 0 :(得分:0)

我认为当您尝试从JSF页面访问您的bean时,第一个字母应该更低:

<h:panelGroup>
    <h:commandButton type="submit" action="#{databaseOperation.submit}"  />
</h:panelGroup>

您也可以设置如下名称:

@ManagedBean(name="databaseOperation")