Results 1 to 16 of 16
Thread: no connection
- 12-18-2007, 02:32 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
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 #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
What the error message that you get when executing the code? What is you database name? is "DB1?" or just "DB1"?
Website: Learn Java by Examples
- 12-28-2007, 10:37 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
no connection
..the DB name is DB1 and the exception message is NullPointerException...
- 12-28-2007, 03:56 PM #4
Member
- Join Date
- Dec 2007
- Posts
- 16
- Rep Power
- 0
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
Java 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);
- 12-28-2007, 04:56 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
...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 #6
Member
- Join Date
- Dec 2007
- Posts
- 16
- Rep Power
- 0
Try printing out the error in your catch() :
Java Code:} catch (SQLException ecc) { System.out.println(ecc); }
- 12-28-2007, 06:19 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
...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 #8
Member
- Join Date
- Dec 2007
- Posts
- 16
- Rep Power
- 0
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 #9
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
...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 #10
Member
- Join Date
- Dec 2007
- Posts
- 16
- Rep Power
- 0
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 #11
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
..no connection
...thanks a lot,anyway...
- 12-29-2007, 03:50 AM #12
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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: Learn Java by Examples
- 12-31-2007, 12:43 PM #13
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
...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 #14
Member
- Join Date
- Dec 2007
- Posts
- 16
- Rep Power
- 0
Have you tried inserting "root" as the username? That's what I use on my localhost.
- 01-02-2008, 10:22 AM #15
Member
- Join Date
- Dec 2007
- Posts
- 8
- Rep Power
- 0
yeah,i've already tried with "root" but nothing to do...
- 01-02-2008, 02:50 PM #16
Member
- Join Date
- Dec 2007
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
connection problem
By subash in forum JDBCReplies: 5Last Post: 04-22-2008, 10:17 AM -
JSP - using connection cache
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:54 AM -
Database connection
By oaklander in forum New To JavaReplies: 2Last Post: 11-13-2007, 12:11 AM -
JDBC connection
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:39 PM -
connection pool for db2
By paty in forum JDBCReplies: 1Last Post: 08-06-2007, 03:43 AM
Bookmarks