Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using java class and variables declared in java file in jsp

843789Jul 26 2009 — edited Jul 28 2009
hi everyone
i m trying to seperate business logic form web layer. i don't know i am doing in a right way or not.
i wanted to access my own java class and its variables in jsp.
for this i created java file like this
package ris;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class  NewClass{
    
    public static void main(String args[]){
        Connection con = null;
        ResultSet rs=null;
        Statement smt=null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
            smt=con.createStatement();
           rs= smt.executeQuery("SELECT * FROM emp");
           while(rs.next()){
            String  str = rs.getString("Name");
           }
            }catch( Exception e){
                String msg="Exception:"+e.getMessage();
            }finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}

        }
    }
}
next i created a jsp where i want to access String str defined in java class above.
<%-- 
    Document   : fisrt
    Created on : Jul 25, 2009, 3:00:38 PM
    Author     : REiSHI
--%>
<%@page import="ris.NewClass"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><%=str%></h1>
    </body>
</html>
I wanted to print the name field extracted from database by ResultSet.
but it gives error cannot find symbol str.
please help me to find right way to do this.
i am using netbeans ide.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 25 2009
Added on Jul 26 2009
2 comments
1,437 views