Jump to content

Help executing mySQL query

- - - - -

  • Please log in to reply
26 replies to this topic

#1
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
hi guys - I started learning java 1 month ago and it's kinda hard to switch from c++ to java(it's easier,but I still can't get used to it) Here my question is....
I have made this connection:

if (txtUsername.getText().equals("ok") && pwd.getText().equals("ok")) {

            String driverName = "com.mysql.jdbc.Driver"; // Start JDBC

            String dbURL = "jdbc:mysql://myserver.com:3306/programm_java";

            String userName = "programm_java";  // windows user

            String userPwd = "12345";  // windows login password

            Connection dbConn;

            try {

                Class.forName(driverName);

                dbConn = DriverManager.getConnection(dbURL, userName, userPwd);

                System.out.println("Connection Successful!");

                lb1.setText("enstablished");

                

            } catch (Exception e) {

                lb1.setText(e.getLocalizedMessage());

            }

The above code is just a connection test.
IT's not working and cant enstablish a connection....How am I supposed to do this :(.
Also how can I execute a SQL query?I didn't managed to find a way...I need to check if the users input match a SQL record,but where and how am I supposed to write the query?! :confused:

PS - I also tried:

private void startConnection()

{

Connection conn = null;

String url = "jdbc:mysql://www.mysqlURL.net:3306";

String dbName = "programm_java";

String driver = "com.mysql.jdbc.Driver";

String userName = "programm_java";

String password = "12345";

try {

  Class.forName(driver).newInstance();

  conn = DriverManager.getConnection(url+dbName,userName,password);

  lb1.setText("Connected to the database");

  conn.close();

  lb1.setText("Disconnected from database");

} catch (Exception e) {

    lb1.setText("NO CONNECTION =(");

}

}

It always sais NO CONNECTION :D


Please help!!!
Any help is highly appreciated!

Edited by alex1, 21 January 2012 - 04:55 AM.


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Can you post the console output you receive from launching this?
If you can't connect to the database, make sure you have access to the database in the first place. (IF you're not hosting locally)
If you have access, make sure the db name, user, and password are correct.

#3
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi Lethal!Thanks for your replay!The message I'm getting from this

} catch (Exception e) {

                lb1.setText(e.getLocalizedMessage());

            }

is: com.mysql.jdbc.Driver

I'm using netbeans and when I'm setting up a db application it makes a successfull connection to the DB...
The problem is that the above codes are not working....I'm sure I'm doing somthing wrong,but I'm 500% sure that the url,the port,db name and password are correct...I really can't understand what's going on - at my point of view the both of the abve codes are ok...

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

} catch (Exception e) {

                lb1.setText(e.getLocalizedMessage());

                [COLOR="#008000"]e.printStackTrace();[/COLOR]

            }

What is the result from adding the green line?

#5
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
its' still showing just: com.mysql.jdbc.Driver :cursing:

- I'm working on a web application btw

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

I'm using netbeans and when I'm setting up a db application it makes a successfull connection to the DB...
You say you are making a successful connection, but turn around and say that those lines of code aren't working.

Where are you able to make the successful connection? And then why are you now not able to make a successful connection?

---------- Post added at 09:39 AM ---------- Previous post was at 09:37 AM ----------

Do you also have the correct driver in your directory? It could be a ClassNotFoundException.

#7
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
here is the success connection to the DB:
img

But I need when the button is pressed to make a new connection and here my nightmare starts....
img

My drivers seems to be ok... :crying:

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Unless you actually have an mysql server available your URL is kinda weird, for developers learning with a local mysql instance running it should be at localhost

#9
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hey wim :) - I have changed the url for security reasons...I just can't understand why it can't connect to the db with the above codes

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay first of all, you should really investigate why the " e.printStackTrace();" as suggested was not working, stack traces are the main thing to help you find the problem.

I've ran it locally with this url and dbname:
String url = "jdbc:mysql://localhost:3306";
String dbName = "myappdb";

And got:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "3306myappdb"'


So at
 conn = DriverManager.getConnection([COLOR=#FF0000]url + dbName[/COLOR], userName, password);
you're simply missing a slash '/' between the url and the dbName.


As for your question how to do queries to the DB:
            ResultSet resultSet = conn.prepareStatement("Select id, name, password from user").executeQuery();
            while(resultSet.next()){
                System.out.println(resultSet.getString("id") + " - " + resultSet.getString("name") + " - " + resultSet.getString("password"));
            }

executeQuery for selects, executeUpdate for update/insert/delete/other stuff

#11
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
hmm if I put "/" it says "Operator cannot be applied " :crying::crying::crying:

#12
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Did you try hosting the database locally?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users