dimanche 1 mai 2016

Unable to call a method of the DAO class

I am trying to build a registration form with a profile image going into the mysql db as a blob.I have a RegisterServlet1.java,a UsersDAO.java,a ConnectionManager.java,a Users.java besides registration.jsp. My mysql db screenshot is attached for the table 'users". I am getting a parse file error for RegisterServlet1.java. More specifically netbeans gives me a hint that

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


See the screenshot attached as it will show the netbeans hint I am getting. Can someone let me know how I will be able to correct the error and get the user details from the form to go into 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 =     "Delta@1600";
        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'




Aucun commentaire:

Enregistrer un commentaire