为所有类java使用一个jdbc连接类

时间:2016-09-17 23:23:54

标签: java

我一直在我的所有必须运行查询的类中使用这个JDBC连接但是我创建了一个新类,我不希望构造函数具有来自JDBC类(主数据库类)的DConnection参数。 但我继续获得NullPointExceptions。无论如何可以弄清楚这个问题可能是什么。 感谢。

// dashboard.es6.jsx
class Dashboard extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      websites: this.props.websites
    }

    // Don't forget to bind to self
    this.handleWebsiteAdd = this.handleWebsiteAdd.bind(this)
  }

  handleWebsiteAdd(newWebsite) {
    const websites = this.state.websites.concat([newWebsite]);

    this.setState({websites});       
  }

  render() {
    return (
      <div className="container">
        <AddWebsite onAdd={this.handleWebsiteAdd}/>
        <WebsiteList websites={this.state.websites}/>
      </div>
    );
  }
}

// add_website.es6.jsx
class AddWebsite extends React.Component {
  constructor() {
    super();
    this.state = {
      formValue: "http://www."
    }
  }

  handleChange(e) {
    this.setState({formValue: e.target.value});
  }

  submitForm() {
    $.ajax({
      method: "POST",
      url: "/websites",
      data: { website: {name: "New Web", url: this.state.formValue} }
    })
      .done(function(data) {
        // NOTE: Make sure that your API endpoint returns new website object
        // Also if your response is structured differently you may need to extract it 
        // and instead of passing data directly, pass one of its properties.
        this.props.onAdd(data)
        Materialize.toast('We added your website', 4000)
      }.bind(this))
      .fail(function() {
        Materialize.toast('Not a responsive URL', 4000)
      });
  }

  render() {
    return (
      <div className="card">
        <div className="input-field" id="url-form">
          <h5>Add new website</h5>
          <form>
            <input id="url-input" value={this.state.formValue} onChange={this.handleChange.bind(this)} type="text" name="url"/>
            <button type="button" onClick={this.submitForm.bind(this)} className="btn">Add Web</button>
          </form>
        </div>
      </div>
    );
  }
}

AddWebsite.propTypes = {
  onAdd: React.PropTypes.func.isRequired
};

像这样的例子,但是在不同的类

中工作
public class UsersDao {

   // associating the Database Connection objekt
    private DConnector connector;

    private final  Connection myConn;
     // Constructor 
    public UsersDao() throws CZeitExceptionHand,SQLException { 
            myConn = connector.getConnenction();
    }
    public boolean updateUsers(String mitarb, int mid) throws SQLException{
        // PreparedStatement myStmt = null;
         Statement stmt = myConn.createStatement();
        try {
         String myStmt = "SELECT Bly "
                        + "" + mid + ";";  
         return stmt.execute(myStmt);

        } finally {
            close(stmt);
        }
    }

1 个答案:

答案 0 :(得分:0)

该对象似乎未初始化。 你能否发布哪种方法不起作用以及从何处调用?

P.S:无法添加评论 - 这就是为什么已经回答了!