|
|
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.
|
|

12-18-2007, 02:32 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
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?
|
|

12-28-2007, 02:29 AM
|
|
Member
|
|
Join Date: Jun 2007
Location: Bali, ID
Posts: 98
|
|
|
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.
|
|

12-28-2007, 10:37 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
no connection
..the DB name is DB1 and the exception message is NullPointerException...
|
|

12-28-2007, 03:56 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 16
|
|
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
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);
|
|

12-28-2007, 04:56 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
...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!
|
|

12-28-2007, 05:19 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 16
|
|
Try printing out the error in your catch() :
}
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.
|
|

12-28-2007, 06:19 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
...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
|
|

12-28-2007, 06:24 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 16
|
|
|
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.
|
|

12-28-2007, 06:30 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
...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...
|
|

12-28-2007, 06:39 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 16
|
|
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!
|
|

12-28-2007, 06:44 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
..no connection
...thanks a lot,anyway...
|
|

12-29-2007, 03:50 AM
|
|
Member
|
|
Join Date: Jun 2007
Location: Bali, ID
Posts: 98
|
|
|
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.
|
|

12-31-2007, 12:43 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
...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...
|
|

12-31-2007, 05:57 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 16
|
|
|
Have you tried inserting "root" as the username? That's what I use on my localhost.
|
|

01-02-2008, 10:22 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 8
|
|
|
yeah,i've already tried with "root" but nothing to do...
|
|

01-02-2008, 02:50 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 28
|
|
|
May be u dont hav class12 jar files
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|