gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:58:45 25 4
gpt4 key购买 nike

我正在尝试构建一个注册表单,其中的个人资料图像作为blob进入mysql数据库。除了registration.jsp之外,我还有一个RegisterServlet1.java、一个UsersDAO.java、一个ConnectionManager.java、一个Users.java。我的 mysql 数据库屏幕截图已附在表“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 数据库吗?

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'

最佳答案

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

由于您的方法 insert 已经是 static,我想 NetBeans 需要重新编译项目才能消除该提示。

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

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

关于java - 无法调用DAO类的方法-非静态方法插入不能从静态上下文中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36974452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com