无法调用DAO类的方法 - 非静态方法插入无法从静态上下文引用

时间:2016-05-02 03:09:15

标签: java compiler-errors

我正在尝试使用配置文件映像构建一个注册表单作为blob进入mysql数据库。我有一个RegisterServlet1.java,一个UsersDAO.java,一个ConnectionManager.java,一个除了registration.jsp之外的Users.java。我的mysql db screenshot附加了表'users'。我收到了RegisterServlet1.java的解析文件错误。更具体地说,netbeans给了我一个暗示

'non static method insert(Users) cannot be referenced from a static context. 
 Unreported exception ClassNotFound Exception must be caught or declared to be thrown'

查看随附的屏幕截图,因为它将显示我正在获得的netbeans提示。 有人能让我知道我将如何纠正错误并从表单中获取用户详细信息进入mysql DB吗?

package com.nszhost.nsztrial;


import java.io.IOException;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig
public class RegisterServlet1 extends HttpServlet {        

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Part fname1= request.getPart("fname");
    Part gender1= request.getPart("gender");
    Part email1= request.getPart("email");
    Part userid1= request.getPart("userid");
    Part password1= request.getPart("password");
    Part profile_img1= request.getPart("profile_img");

    Scanner scanner1  = new Scanner( fname1.getInputStream());
    String fname2= scanner1.nextLine();
    Scanner scanner2  = new Scanner( gender1.getInputStream());
    String gender2= scanner2.nextLine();
    Scanner scanner3  = new Scanner( email1.getInputStream());
    String email2= scanner3.nextLine();
    Scanner scanner4  = new Scanner( userid1.getInputStream());
    String userid2= scanner4.nextLine();
    Scanner scanner5  = new Scanner( password1.getInputStream());
    String password2= scanner5.nextLine();






    Users user=new Users();

    user.setEmail(email2);
    user.setFname(fname2);
    user.setGender(gender2);
    user.setPassword(password2);
    user.setUserid(userid2);
    user.setProfile_img((Part) profile_img1);



        UsersDAO.insert(user);





}



}
package com.nszhost.nsztrial;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class UsersDAO {

private static Connection conn = ConnectionManager.getInstance().getConnection();
public static void insert(Users user) throws ClassNotFoundException, SQLException, IOException{
    Class.forName("com.mysql.jdbc.Driver");

PreparedStatement ps;
ps = conn.prepareStatement("insert into users (fname,gender,email,userid,password,profile_img) values(?,?,?,?,?,?)");

ps.setString(1, user.getFname());
ps.setString(2, user.getGender());
ps.setString(3, user.getEmail());
ps.setString(4, user.getUserid());
ps.setString(5, user.getPassword());


        // size must be converted to int otherwise it results in error
ps.setBinaryStream(6, user.getProfile_img().getInputStream(), (int)  user.getProfile_img().getSize());
ps.executeUpdate();
conn.commit();


    }
}
package com.nszhost.nsztrial;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {

        private static ConnectionManager instance = null;

        private final String USERNAME =     "root";
        private final String PASSWORD =     "****";
        private final String CONN_STRING = "jdbc:mysql://localhost/jdbc";

        private Connection conn = null;

        private ConnectionManager(){
        }

        public static ConnectionManager getInstance(){
            if(instance == null){
                instance = new ConnectionManager();
            }
            return instance;
        }
        private boolean openConnection(){
            try {
                conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return true;
        }
        public Connection getConnection(){
                if(conn == null){
                    if(openConnection()){
                            System.out.println("Connection Opened");
                            return conn;
                    }else{
                        return null;
                    }
                }
                return conn;
        }
        public void close(){
            System.out.println("Closing Connection");
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn = null;
        }
}
package com.nszhost.nsztrial;

import javax.servlet.http.Part;

public class Users {


private int id;
private String fname;
private String gender;
private String email;
private String userid;      
private String password;   
private Part profile_img;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFname() {
    return fname;
}

public void setFname(String fname) {
    this.fname = fname;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}



public Part getProfile_img() {
    return profile_img;
}

public void setProfile_img(Part profile_img) {
    this.profile_img = (Part)  profile_img;
}



}

netbeans hint screenshot mysql table 'users'

1 个答案:

答案 0 :(得分:1)

我认为这是某种教程,所以我会忽略代码中的不同设计缺陷。

由于您的方法<xsl:template match="*[@shortname or @refname]"> 已为insert,我认为NetBeans需要重新编译项目才能摆脱该提示。

至于static: 您声明了方法ClassNotFoundException。由于public static void insert(Users user) throws ClassNotFoundException [...]是一个经过检查的异常,因此您必须重新抛出或捕获异常。我想按下alt + enter(如你的屏幕截图中的NetBeans建议)会向你显示这两个选项。

如果您不确定已检查和未检查的异常,我建议您阅读一些有关它们的内容,因为它们是java语言中的核心概念。 This似乎是一个很好的简短解释。