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

  • Home
  • SEARCH
  • 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 4035720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:08:02+00:00 2026-05-20T12:08:02+00:00

I’m trying to do a simple insert query in mysql using java. How do

  • 0

I’m trying to do a simple insert query in mysql using java. How do I determine what the problem is if its not doing what its supposed to do:

import java.util.*;
import java.sql.*;
import javax.swing.JOptionPane;

public class regp {

    public regp(String player, int score) {
        conc conclass = new conc();
        Connection conn = conclass.dbConnect();


        try {



            PreparedStatement fetchPlayers = conn.prepareStatement("SELECT * FROM players WHERE P_Name='" + player + "'");
            ResultSet rs = fetchPlayers.executeQuery();

            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Already Registered!");
            } else {


                PreparedStatement createPlayer = conn.prepareStatement("INSERT INTO players(P_Name, Score) VALUES('" + player + "', '" + score + "')");

                createPlayer.execute();




                JOptionPane.showMessageDialog(null, "Player: " + player + " score: " + score);


                JOptionPane.showMessageDialog(null, "Registration Successful!");

                JOptionPane.showMessageDialog(null, "Not registered!");



            }
        } catch (Exception e) {
        }

    }
}

And heres conc.java which contains the database information:

import java.sql.*;
import java.util.*;

public class conc {

    public conc(){
    }

       public Connection dbConnect() {
        try {
            String db_connect_string="jdbc:mysql://localhost:3306/questions";
            String db_userid="root";
            String db_password="1234";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);


            return conn;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Problem is I don’t have any idea if its the query, the prepared statement declaration.
One thing is sure to be working. The part which checks if the player inputted is existing or not.
I already checked if the function is getting the values needed by outputting them using joptionpane message dialog. And it sure receives the correct values. So I’m thinking that maybe the problem is in the query.

  • 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-20T12:08:02+00:00Added an answer on May 20, 2026 at 12:08 pm

    You could print the exception that you’re catching in your regp constructor:

    } catch (Exception e) {
        e.printStackTrace();
    }
    

    You may also want to look at prepared statements rather than constructing your SQL via string concatenation.

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

Sidebar

Related Questions

No related questions found

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.