Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6095073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:46:19+00:00 2026-05-23T12:46:19+00:00

This is a simple program where i create two tables in a database using

  • 0

This is a simple program where i create two tables in a database using jdbc and sql.

Connection con=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","root");
try
{
Statement st=con.createStatement();
st.executeUpdate("CREATE DATABASE IF NOT EXISTS ARCPAGE");
st.executeUpdate("USE ARCPAGE");
st.executeUpdate("CREATE TABLE IF NOT EXISTS User(user_id integer NOT NULL AUTO_INCREMENT,user_name varchar(20) NOT NULL,user_pass varchar(20) NOT NULL,PRIMARY KEY (user_id))");
st.executeUpdate("CREATE TABLE IF NOT EXISTS Usermap(user_name varchar(20) NOT NULL,role_name varchar(20) NOT NULL)");
ResultSet res=st.executeQuery("SELECT * FROM USER");
res.next();
ResultSet resmap=st.executeQuery("SELECT * FROM USERMAP WHERE USER_NAME='"+res.getString("user_name")+"'");
/*error line */ res.next();
resmap=st.executeQuery("SELECT * FROM USERMAP WHERE USER_NAME='"+res.getString("user_name")+"'");
}
catch(SQLException e)
{
    System.out.println("SQL ERROR");
}
}
catch(Exception e)
{
    System.out.println("ERROR");
}

on executing the code i get an “SQL ERROR” from the catch block.on further investigation i see that the error is from the /error line/ which i marked. what am i doing wrong???

-krishan

on e.printstacktrace()

java.sql.SQLException: Operation not allowed after ResultSet closed
    at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:652)
    at com.mysql.jdbc.ResultSet.next(ResultSet.java:5942)
    at trees.TreeBean.<init>(TreeBean.java:51)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:184)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:98)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:405)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:191)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:73)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:112)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:164)
    at org.primefaces.util.ComponentUtils.getStringValueToRender(ComponentUtils.java:61)
    at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:86)
    at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:53)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:879)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:308)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:181)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:125)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:849)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1643)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:164)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:849)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1643)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1646)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1646)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1646)
    at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:180)
    at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:120)
    at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:61)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:879)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1646)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1646)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:389)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:127)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:117)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:46:19+00:00Added an answer on May 23, 2026 at 12:46 pm

    Lots of stuff wrong with this code:

    1. Prefer PreparedStatement to Statement.
    2. Don’t create SQL by concatenating Strings; bind variables with “?”
    3. You don’t close any of your Connection, Statement, or ResultSet assets.
    4. Print the stack trace in catch blocks. Your message is no information at all.
    5. You look like you want to JOIN results from User and UserMap, but this is an inefficient (n+1) query way to do it. Better to do a JOIN.
    6. Shouldn’t you create tables in one method and query in another?

    I don’t understand wny you have statements to create the database every time and create those tables. Shouldn’t that be done before you run these Java methods? Your code isn’t making much sense to me.

    I’d write it more like this. I didn’t test it, but it does compile.

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    
    public class SqlDemo {
    
        private static final String SELECT_USER_ROLE_SQL =
                "SELECT m.user_name, m.user_role " +
                "FROM User as u " +
                "JOIN Usermap as m" +
                "ON u.user_name = m.user_name ";
    
        public Map<String, String> getAllUserRoles(Connection connection) {
            Map<String, String> userRoles = new LinkedHashMap<String, String>();
    
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            try {
                ps = connection.prepareStatement(SELECT_USER_ROLE_SQL);
                rs = ps.executeQuery();
                while (rs.next()) {
                    String user = rs.getString("user_name");
                    String role = rs.getString("user_role");
                    userRoles.put(user, role);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                close(rs);
                close(ps);
            }
    
            return userRoles;
        }
    
        private static void close(ResultSet rs) {
            try {
                if (rs != null) rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void close(Statement st) {
            try {
                if (st != null) st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have a simple script which is used to start another program. This other
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
I'm using this simple regular expression to validate a hex string: ^[A-Fa-f0-9]{16}$ As you
This is a simple program that draws a 3d cube with a texture. The
I wrote a simple daemon. This daemon should respond when I run any program.
This simple code is not producing any sound on a couple of machines that
I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
I have this simple regex, [\d]{1,5} that matches any integer between 0 and 99999.
I have this simple Jsp page: <%@ page language=java import=java.awt.Color%> <% Color background =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.