• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java string sql

 
Brandon Tisson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a social app in android studio but I have a problem . I can create multiple users with one username but I don't want that to happen I want each user to have an individual username but I don't know how to do that . I am using 000webhost`so I don't know if the following code I wrote would work . So can someone help me ?

String sql = "SELECT username FROM users WHERE username = username";

if()
{
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you executing that database statement? (Which has issues, but one thing at a time.)
 
Knute Snortum
Bartender
Pie
Posts: 2878
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can enforce uniqueness on a column in the DB table itself.  Then what you can do is respond to any uniqueness exception and ask the user to enter another username.
 
Norm Radder
Bartender
Posts: 1526
14
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

KS is right; you are probably best using the database to enforce unique user names. I shall try duplicating this discussion in our databases forum.
 
Brandon Tisson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using 000webhost which I was just told doesn't work properly . But this is the code I use to register the user. My register code :

$username = $_POST["username"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "INSERT INTO users (username, password) VALUES (?, ?)");

    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);
   
    $response = array();
    $response["success"] = true; 
   
    print_r(json_encode($response));

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That certainly is not Java.
 
Brandon Tisson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I know . That's the PHP I used to connect my app to the database .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so now I'm confused. Are you planning on using Java or PHP for this? Are you looking for the Java equivalent to the PHP you posted?
 
Brandon Tisson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using android studio(Java) to create the app , but i'm using php to connect to my database.
 
Paul Clapham
Sheriff
Posts: 21548
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And... you want to change that so you're using Java for everything? Or am I misunderstanding your goal?
 
Brandon Tisson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to make it a little more clear . I am creating a social app with android studio (Java) I am using PHP to connect to a database . As I am creating this app and checking if I can properly sign users up with no problem I noticed something that I wanted to change but didn't know how . Multiple usernames can be signed up and I want only one username to be available and if it's taken then the user will have to choose a different username .
 
Paul Clapham
Sheriff
Posts: 21548
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but that doesn't improve my understanding of your requirements at all. I understand the part where you don't want to have two people choose the same user name, that was clear from the start, but the rest is still a mystery to me.

So let's try some direct questions. Do you plan to replace that PHP code by some Java code? Or do you plan to incorporate it into your Java app code somehow?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am guessing you're in this bit of the forum as there isn't (or at least I can't see) any general database sub-forum.

As said higher up, you should create the users table such that username is a UNIQUE column.
That way, when you try and INSERT a new row that has a name that has been used the database will throw an error that you can catch and handle.  No idea how you do that in PHP, but there is a PHP sub-forum further down the list.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java®, you would find that the update methods all declare a (checked) SQLException, which will carry details of any errors in the SQL.

This question has already been duplicated to the databases forum. Please confirm whether you want to use Java® or stick with the PHP; I might have to move the discussion again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic