如何通过调用 Java 类的方法从 JSP 中使用 Getter 方法获取数据?

huangapple 未分类评论77阅读模式
英文:

How should i get data by calling javaclass method from JSP using Getters Method?

问题

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="images"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_231">
            <attributes>
                <attribute name="owner.project.facets" value="java"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.0">
            <attributes>
                <attribute name="owner.project.facets" value="jst.web"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
        <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
        <classpathentry kind="lib" path="D:/BABU/my_soft/jar_files/ojdbc14.jar"/>
        <classpathentry kind="lib" path="D:/BABU/my_soft/jar_files/mysql-connector.jar"/>
        <classpathentry kind="lib" path="D:/BABU/my_soft/jar_files/mysql-connector-java-5.1.23-bin.jar"/>
        <classpathentry kind="output" path="build/classes"/>
    </classpath>
// JSP Code where I'm trying to call Methods getCid(),getName()

<%@page import="daopack.DaoData"%>
<%@page import="mypack.FetchData" %>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>addcartdb</title>
</head>
<body>
<%
try {
    
    String pname = request.getParameter("pname");
    int pprice = Integer.parseInt(request.getParameter("pprice"));
    int tq = Integer.parseInt(request.getParameter("tq"));
    
    String mail = (String)session.getAttribute("mail");
    

    
    DaoData dd = new DaoData();
    
    String cid = dd.getCid(mail);;
    
    
     System.out.print(" addcartdb..cid...: "+cid+"\n");
     
     String name2 = dd.getName(mail);
     
     System.out.print("\n addcartdb..name...: "+name2+"\n");
    
    String url ="jdbc:mysql://localhost:3306/srm";
    String user = "root";
    String password = "root";
    String sql=" insert into customerstock(cid,name,pname,pprice,tq)values(?,?,?,?,?)";
    
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url, user, password);
    
    
    
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, cid);
    ps.setString(2, name2);
    ps.setString(3, pname);
    ps.setInt(4, pprice);
    ps.setInt(5, tq);
    
    
    int i = ps.executeUpdate();
    out.print(" executed  "+i);
    if (i > 0) {
        out.print("<script>");
        out.print("alert('Added..!'); window.location.href='add_cart.jsp';");
        out.print("</script>");

    }
// }

} catch (Exception e) {

    System.out.println(e);
}

System.out.println(" printed...Addcartdb....3");


%>
</body>
</html>
// DaoData.java file

package daopack;

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

public class DaoData {

    private static Connection getConnection() throws ClassNotFoundException, SQLException{
        
        String url ="jdbc:mysql://localhost:3306/srm";
        String user = "root";
        String password = "root";
        Class.forName("com.mysql.jdbc.Driver");
        //Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection con=DriverManager.getConnection(url,user,password);
      
        return con;
        
    }

    private String cid;
    private String name;

    public String getCid(String mail) throws ClassNotFoundException, SQLException {
        Connection con = getConnection();
        if(con!=null)
            System.out.println("\n Connected...!  "+con+"\n");
        PreparedStatement ps = con.prepareStatement("select cid from registar where mail='"+mail+"'");
        ResultSet rs= ps.executeQuery();
        while(rs.next()){
            cid=rs.getString("cid");
        }
        
        con.close();
        return cid;
    }

    public String getName(String mail) throws ClassNotFoundException, SQLException {
        
        System.out.println("\nDaoData---:"+mail+"\n");
        Connection con = getConnection();
        if(con!=null)
            System.out.println("\n Connected...!  "+con+"\n");  
        PreparedStatement ps = con.prepareStatement("select cid from registar where mail='"+mail+"'");
        ResultSet rs= ps.executeQuery();
        while(rs.next()){
            name=rs.getString("cid");
        }
        
        con.close();
        
        return name;
    }
}
英文:

>.Classpath file
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;classpath&gt;
	&lt;classpathentry kind=&quot;src&quot; path=&quot;src&quot;/&gt;
	&lt;classpathentry kind=&quot;src&quot; path=&quot;images&quot;/&gt;
	&lt;classpathentry kind=&quot;con&quot; path=&quot;org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_231&quot;&gt;
		&lt;attributes&gt;
			&lt;attribute name=&quot;owner.project.facets&quot; value=&quot;java&quot;/&gt;
		&lt;/attributes&gt;
	&lt;/classpathentry&gt;
	&lt;classpathentry kind=&quot;con&quot; path=&quot;org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.0&quot;&gt;
		&lt;attributes&gt;
			&lt;attribute name=&quot;owner.project.facets&quot; value=&quot;jst.web&quot;/&gt;
		&lt;/attributes&gt;
	&lt;/classpathentry&gt;
	&lt;classpathentry kind=&quot;con&quot; path=&quot;org.eclipse.jst.j2ee.internal.web.container&quot;/&gt;
	&lt;classpathentry kind=&quot;con&quot; path=&quot;org.eclipse.jst.j2ee.internal.module.container&quot;/&gt;
	&lt;classpathentry kind=&quot;lib&quot; path=&quot;D:/BABU/my_soft/jar_files/ojdbc14.jar&quot;/&gt;
	&lt;classpathentry kind=&quot;lib&quot; path=&quot;D:/BABU/my_soft/jar_files/mysql-connector.jar&quot;/&gt;
	&lt;classpathentry kind=&quot;lib&quot; path=&quot;D:/BABU/my_soft/jar_files/mysql-connector-java-5.1.23-bin.jar&quot;/&gt;
	&lt;classpathentry kind=&quot;output&quot; path=&quot;build/classes&quot;/&gt;
&lt;/classpath&gt;

<!-- end snippet -->

** getting data from DB using java class with two methods getCid(),getName() But i'm getting error.what should i do?**
CLICK HERE to image-This is the page where i'm adding products from ADMIN_DB to User_DB

CLICK HERE to image-This is the Error-code getting. After by clicking ADD button in Addingpage

>ERROR-PAGE

HTTP Status 500 - Unable to compile class for JSP:
type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [14] in the generated java file: [C:\Users\HCL\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\myregistration\org\apache\jsp\addcartdb_jsp.java]
Only a type can be imported. daopack.DaoData resolves to a package

An error occurred at line: [15] in the generated java file: [C:\Users\HCL\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\myregistration\org\apache\jsp\addcartdb_jsp.java]
Only a type can be imported. mypack.FetchData resolves to a package

An error occurred at line: 29 in the jsp file: /addcartdb.jsp
DaoData cannot be resolved to a type
26: 
27: 	//SelectData sd = new SelectData();
28: 	//FetchData fd = new FetchData();
29: 	DaoData dd = new DaoData();
30: 	
31: 	String cid = dd.getCid(mail);;
32: 	


An error occurred at line: 29 in the jsp file: /addcartdb.jsp
DaoData cannot be resolved to a type
26: 
27: 	//SelectData sd = new SelectData();
28: 	//FetchData fd = new FetchData();
29: 	DaoData dd = new DaoData();
30: 	
31: 	String cid = dd.getCid(mail);;
32: 	


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:467)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:380)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:355)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:342)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.3 logs.

Apache Tomcat/8.0.3

>JSP Code where i'm trying to call Methods getCid(),getName()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;%@page import=&quot;daopack.DaoData&quot;%&gt;
&lt;%@page import=&quot;mypack.FetchData&quot; %&gt;

&lt;%@page import=&quot;java.sql.PreparedStatement&quot;%&gt;
&lt;%@page import=&quot;java.sql.ResultSet&quot;%&gt;
&lt;%@page import=&quot;java.sql.DriverManager&quot;%&gt;
&lt;%@page import=&quot;java.sql.Connection&quot;%&gt;
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
&lt;title&gt;addcartdb&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
try {
    
	String pname = request.getParameter(&quot;pname&quot;);
	int pprice = Integer.parseInt(request.getParameter(&quot;pprice&quot;));
	int tq = Integer.parseInt(request.getParameter(&quot;tq&quot;));
	
	String mail = (String)session.getAttribute(&quot;mail&quot;);
	

	
	DaoData dd = new DaoData();
	
	String cid = dd.getCid(mail);;
	
	
	 System.out.print(&quot; addcartdb..cid...: &quot;+cid+&quot;\n&quot;);
	 
     String name2 = dd.getName(mail);
     
     System.out.print(&quot;\n addcartdb..name...: &quot;+name2+&quot;\n&quot;);
	
	String url =&quot;jdbc:mysql://localhost:3306/srm&quot;;
	String user = &quot;root&quot;;
	String password = &quot;root&quot;;
	String sql=&quot; insert into customerstock(cid,name,pname,pprice,tq)values(?,?,?,?,?)&quot;;
	
	Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
	Connection con = DriverManager.getConnection(url, user, password);
	
	
	
	PreparedStatement ps = con.prepareStatement(sql);
	ps.setString(1, cid);
	ps.setString(2, name2);
	ps.setString(3, pname);
	ps.setInt(4, pprice);
	ps.setInt(5, tq);
	
	
	int i = ps.executeUpdate();
	out.print(&quot; executed  &quot;+i);
	if (i &gt; 0) {
		out.print(&quot;&lt;script&gt;&quot;);
		out.print(&quot;alert(&#39;Added..!&#39;); window.location.href=&#39;add_cart.jsp&#39;;&quot;);
		out.print(&quot;&lt;/script&gt;&quot;);

	}
//}

} catch (Exception e) {

	System.out.println(e);
}

System.out.println(&quot; printed...Addcartdb....3&quot;);


%&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->
>DaoDta.java file

package daopack;

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

public class DaoData {

private static Connection getConnection() throws ClassNotFoundException, SQLException{
		
		String url =&quot;jdbc:mysql://localhost:3306/srm&quot;;
		String user = &quot;root&quot;;
		String password = &quot;root&quot;;
		Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
        //Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);  
      Connection  con=DriverManager.getConnection(url,user,password);
      
		return con;
		
	}

private String cid;
private String name;

public String getCid(String mail) throws ClassNotFoundException, SQLException {
	Connection con = getConnection();
	if(con!=null)
	System.out.println(&quot;\n Connected...!  &quot;+con+&quot;\n&quot;);
	 PreparedStatement ps = con.prepareStatement(&quot;select cid from registar where mail=&#39;&quot;+mail+&quot;&#39;&quot;);
		ResultSet rs= ps.executeQuery();
		while(rs.next()){
		cid=rs.getString(&quot;cid&quot;);
		}
		
		con.close();
	return cid;
}

public String getName(String mail) throws ClassNotFoundException, SQLException {
	
	System.out.println(&quot;\nDaoData---:&quot;+mail+&quot;\n&quot;);
	Connection con = getConnection();
	if(con!=null)
		System.out.println(&quot;\n Connected...!  &quot;+con+&quot;\n&quot;);	
	 PreparedStatement ps = con.prepareStatement(&quot;select cid from registar where mail=&#39;&quot;+mail+&quot;&#39;&quot;);
		ResultSet rs= ps.executeQuery();
		while(rs.next()){
		name=rs.getString(&quot;cid&quot;);
		}
		
		
	con.close();
	
	return name;
}



}


huangapple
  • 本文由 发表于 2020年8月14日 20:30:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/63412796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定