Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-18-2007, 02:32 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
no connection
Hi,i'm trying to extabilish a connection with a Mysql database.
The line to load the driver should be:

Class.forName("com.mysql.jdbc.Driver").newInstance ();

Once you loaded the driver the line to connect should be:

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB1?","", "password");

it doesn't work at all.
Does anyone know the reason? I don't have a username since Mysql doesn't require it,could the "blank space" argument be the problem?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-28-2007, 02:29 AM
Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 98
wsaryada is on a distinguished road
What the error message that you get when executing the code? What is you database name? is "DB1?" or just "DB1"?
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-28-2007, 10:37 AM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
no connection
..the DB name is DB1 and the exception message is NullPointerException...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-28-2007, 03:56 PM
Member
 
Join Date: Dec 2007
Posts: 16
Cazrin is on a distinguished road
Looking at your code your database name is wrong, like wsaryada said, you've got "DB1?" written in, whereas it should be "DB1". Everything else seems fine, although you could try setting up a user and seeing if that's causing the problem.

Also, maybe you don't need to include the port. The slice of code I found doesn't, and I'm not sure if it's required

Code:
String userName = "testuser"; String password = "testpass"; String url = "jdbc:mysql://localhost/test"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-28-2007, 04:56 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
...no connection
nothing to do,here's the whole code...it's about a small DB table to menage phone numbers...i've used "PreparedStatement" to implement the three op of the application...

package accessidb;
import java.lang.Exception;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LoadDriver // this is an external class just to load the driver
{
public LoadDriver() { }
public void load()
{
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance ();
} catch (Exception ex) {
// handle the error
}
}
}

public class Sql {


private Connection con;



/** Creates a new instance of Sql */
public Sql()
{
try {
LoadDriver LD = new LoadDriver();
LD.load();
String URL = "jdbc:mysql://localhost/db1";
String user = " ";
String pass = "wordpass";
con = DriverManager.getConnection(URL,user,pass);


}
catch (SQLException ecc) { }


}

public void insert (String name,String surname,int phone)
{

try {
PreparedStatement ins = con.prepareStatement("INSERT INTO Rubrica (Name,Surname,Phone) VALUES (?, ?, ?)");
ins.setString(1,name);
ins.setString(2,surname);
ins.setInt(3,phone);

}
catch (SQLException ecc) { }
}

public void update (String name,String surname,int newphone)
{
try {
PreparedStatement update = con.prepareStatement("UPDATE Rubrica SET Phone = ? WHERE Name = ? AND Surname = ? ");
update.setInt(1,newphone);
update.setString(2,name);
update.setString(3,surname);

}
catch (SQLException ecc) { }

}

public void delete (String name,String surname)
{
try {

Statement delete = con.createStatement();
delete.execute("DELETE FROM Rubrica WHERE Name = '" + name + "' AND Surname = '" + surname + "'" );


}
catch (SQLException ecc) { }

}


}

...nothing to do...it's like it can't find the DB at all!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-28-2007, 05:19 PM
Member
 
Join Date: Dec 2007
Posts: 16
Cazrin is on a distinguished road
Try printing out the error in your catch() :

Code:
} catch (SQLException ecc) { System.out.println(ecc); }
See what it outputs and you can test that at each point to see where it's failing.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-28-2007, 06:19 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
...no connection
...it fails when it's trying to read the DB entries...i tried to debug it and it fails there...it compile the statement

con = DriverManager.getConnection(URL,user,pass);

but it finds an error at the line

Statement delete = con.createStatement();

it's like the connection done at the first line didn't exist...bye
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-28-2007, 06:24 PM
Member
 
Join Date: Dec 2007
Posts: 16
Cazrin is on a distinguished road
Should Statement delete = con.createStatement(); not be PreparedStatement delete = con.createStatement(); like your other lines?

I'm no JDBC or SQL expert, nor novice, but if your code is failing at that line, that's the only difference I can see.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-28-2007, 06:30 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
...no connection
...oh no the syntax is right...i've done a test just to delete a row of the table...but the problem is the connection...
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-28-2007, 06:39 PM
Member
 
Join Date: Dec 2007
Posts: 16
Cazrin is on a distinguished road
I wish I could help more but this seems to be out of my knowledge, I haven't started to touch JDBC yet. Although I've found this URL, which seems to use a different method for executing queries:

How to use JDBC

Hopefully that can help you where I can't!
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-28-2007, 06:44 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
..no connection
...thanks a lot,anyway...
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-29-2007, 03:50 AM
Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 98
wsaryada is on a distinguished road
Hello, have you solve the problem?

As cazrin said, you can put something like e.printStackTrace() in your catch block, so it will print the error message when something is happening.

The NPE could be caused by the connection, but prior to that what make the connection null in the first place. It could be the database name is wrong, the user name or password to connection to database might be invalid, or event the database is not running.
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-31-2007, 12:43 PM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
...no connection
...i tried to print the StuckTrace,it seems to be the missing username,but when it configures mysql username it's not required,it can leave "root administrator" and insert the password only...that's the reason why i inserted an empty string for username...i've to configure mysql again...
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-31-2007, 05:57 PM
Member
 
Join Date: Dec 2007
Posts: 16
Cazrin is on a distinguished road
Have you tried inserting "root" as the username? That's what I use on my localhost.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-02-2008, 10:22 AM
Member
 
Join Date: Dec 2007
Posts: 8
even is on a distinguished road
yeah,i've already tried with "root" but nothing to do...
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-02-2008, 02:50 PM
Member
 
Join Date: Dec 2007
Posts: 28
anki1234 is on a distinguished road
May be u dont hav class12 jar files
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
connection problem subash Database 5 04-22-2008 10:17 AM
JSP - using connection cache Java Tip Java Tips 0 01-30-2008 10:54 AM
Database connection oaklander New To Java 2 11-13-2007 12:11 AM
JDBC connection Java Tip Java Tips 0 11-10-2007 08:39 PM
connection pool for db2 paty Database 1 08-06-2007 03:43 AM


All times are GMT +3. The time now is 07:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org