File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection" Watch "Connection" New topic
Author

Connection

nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
Hi all,
I connect to MySQL server using just
C:\mysql\bin\mysql <enter>

In the java code I have given this,
String url = "jdbc:mysql://localhost:3306/myfirstdatabase/nash";
Where "myfirstdatabase" is the database and "nash" is the table name.
Is this the correct way to do and to connect to the database I give the following
Connection con = DriverManager.getConnection(url);
Is this the way.
After this,I get this error,
/*
java.lang.NullPointerException
at DatabaseConnection$1.actionPerformed(DatabaseConnection.java:84)
*/
where DatabaseConnection.java is the file name.
Kindly guide me in the correct direction
Thanks
nash
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yoy want to connect to the database. Try it without including the table name in the URL. Once the connection to the database is established, you can access your tables.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
hi,
Thanks for the reply.As directed,I ran the program without giving the table name,but the error persists.
Help would be appreciated
nash
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I would double check that the database "myfirstdatabase" exists and you can select from one of its tables
nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
Thanks for the reply,The database "myfirstdatabase" exists and as a matter of fact i am trying to create a table using Java,there are no tables in "myfirstdatabase".
let me know what could be the problem
nash
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Also verify that mysql server is up and running before executing via java. I accesed the user table in the mysql db that came with the server with the following. Note: I don't have the port number. I also tried with the port number and it worked.
Class.forName("org.gjt.mm.mysql.Driver");
Connection databaseConnection = DriverManager.getConnection("jdbc:mysql://localhost/mysql");
Statement statement = databaseConnection.createStatement();
ResultSet myRs = statement.executeQuery(
"SELECT Host,User, Password FROM user");
nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
hi,
Thanks for the continual help.I figured out the way the connection is established.
I have a further doubt on this line.
/*Connection conn = DriverManager.getConnection(url)*/.
How do I give a userid and password in the above line so that I can connect to the MySql server.

Kindly do let me know.Any help would be appreciated.
nash
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8996
    
    9

If one looks at the API documentation for java.sql.DriverManager
they would see a number of methods that may fit the bill, such as:
public static Connection getConnection(String url, Properties info)
or
public static Connection getConnection(String url, String user, String password)
You do realize the line you cite above is commented out, right?
[ December 01, 2003: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
thanks for the reply.
I do know that the code is commented out.
I understand that there are a lot of methods to do the needful,but....This is my question
The sql server is running on my local win xp home machine and I conect to it using this
Database is : MySql 4.0.15-nt
c:\mysql\lib\mysql
Thats it.
Now I want to know how the userid and password is given in the below line.
FYI,I dont know how a connection to the database can be done by given Userid and password.
Connection conn = DriverManager.getConnection(url)
Do let me know.
Thanks for the help
nash
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8996
    
    9

Originally posted by Joe Ess:
If one looks at the API documentation for java.sql.DriverManager
they would see a number of methods that may fit the bill, such as:
public static Connection getConnection(String url, Properties info)
or
public static Connection getConnection(String url, String user, String password)

Hmmm. If I saw a comment like this, and I were looking for a method of java.sql.DriverManager that returned a connection using a url, a user name, and a password, I would make a leap of faith and guess that the method that has three arguments named url, user, and password was the one I was looking for. And if I had any questions I would follow the convenient link the submitter provided to the API documentation. But that's just me.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connection