• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would double check that the database "myfirstdatabase" exists and you can select from one of its tables
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic