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 7819935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:07:13+00:00 2026-06-02T07:07:13+00:00

I had been improve my knowledge about java for 2 years.Today, I was started

  • 0

I had been improve my knowledge about java for 2 years.Today, I was started working with databases.I have 2 class that are DatabaseConnection and UserModel.Databaseconnection class have connecting operations and UserModel will add,remove,update etc. methods.I didn’t write remove and update code’s yet.I writed only add method.But It its adding to user.I searched many thinks about that but i cant fixed that.

This is my Connection Class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author KHn
 */
public abstract class DatabaseConnection {
    private Connection connect;
    private static final String dbUsername="root";
    private static final String dbPassword="";
    private static final String dbName="virtualblog";
    private static final String dbHost="jdbc:mysql://localhost:3306/";
    private String driver="com.mysql.jdbc.Driver";
    private Statement st;
    private ResultSet rs;
    /** Creates a new instance of DatabaseConnection */
    public DatabaseConnection(){
        try { 
        Class.forName(this.driver).newInstance();
        setConnect((Connection)DriverManager.getConnection(dbHost+dbName,dbUsername,dbPassword));

        } catch (Exception e) {
            System.out.println(e);
        }
    }
    public Connection getConnect() {
        return connect;
    }

    public void setConnect(Connection connect) {
        this.connect = connect;
    }



}

More Over, this is a UserModelClass:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.beans.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

    /**
 *
 * @author KHn
 */
@ManagedBean
@RequestScoped
public class UserModel extends DatabaseConnection{
    private PreparedStatement ps;
    private Statement st;
    private ResultSet ts;
    Register member =new Register();
    public Register getUser() {
        return member;
    }
    public void setUser(Register member) {
        this.member = member;
    }
    public boolean addUser(){
        try{
            ps=(PreparedStatement)getConnect().prepareStatement("INSERT INTO `members`(`id`, `username`, `email`, `password`, `name`, `avatar`, `blogname`) VALUES (null,?,?,'3694406','kaan','asdasd','blog adi')");
            ps.setString(2, member.getUsername());
            ps.setString(3, member.getEmail());
            ps.executeUpdate();
        }catch (Exception e) {
            System.err.println("ekleme hatasi: "+e);
        }
        return true;
    }
/** Creates a new instance of UserModel */
    public UserModel() {
    }
}

Where did i do wrong? Can you help me about that please.Thanks and Best Regards.

  • 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-06-02T07:07:16+00:00Added an answer on June 2, 2026 at 7:07 am

    Your JDBC connection may be in transactional mode. I.e. (multiple) queries passed through the connection will only be really executed (in an isolation) when the connection is committed (or closed).

    So, calling connection.commit() or connection.close() should do it.

    You’ve only a major design mistake in your code which makes it hard to implement this properly. You basically need to rewrite everything. To start, you should absolutely not have expensive DB resources like Connection, Statement and ResultSet as instance variables of a class. They should be declared, acquired and closed in the shortest possible scope, preferably in the very same method block as where you’re executing the SQL query. Also, all that JDBC job belongs in a separate DAO class, not in a managed bean. Your DAO code needs to be designed that way so that you ultimately end up like follows in your addUser() method:

    public void addUser() throws SQLException {
        memberDAO.create(member);
    }
    

    To get a step further, consider learning EJB/JPA as well. EJBs allows for finer grained control over transactions and have a sane default behaviour (auto-commit by end of request, for example) and JPA results in less JDBC boilerplate code, all the JDBC code can basically be replaced by oneliners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of the products which I have been working started some 10 years back.
I had been working on server side(c#) for a couple of years. But now
I've been a Java developer for many years but never had to deal too
Actually i had been practicing some of the java programs today(trying to learn it
I just started working with jQuery and I have been searching for some type
From previous experience I had been under the impression that it's perfectly legal (though
I have had been on a roller coaster trying to get ImageMagick to work
I am using Microsoft SQL Server. I have a Table which had been updated
A couple of years ago, before I knew about Stack Overflow, I was working
I'm trying to improve our build process, and to that end I've been looking

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.