Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

not able to connect to mysql database with eclipse

843859Nov 30 2007 — edited Nov 20 2014
hi everybody,
i have a question here.
i am trying to connect to mysql database with eclipse. i had downloaded the mysql-connector-java-5.1.5 file. i am able to load the class but the next line drivermanager.getconnection is not working. it is not throwing any exception. if i change the database name then it shows "unknown database" error.
my code is:



import java.sql.*;
public class jdbcclass
{
public static void main(String[] args)
{

Connection con=null;
Statement st=null;
ResultSet rs=null;
try
{

String username="admin";
String password="admin";
String url = "jdbc:mysql://localhost:3306/lushdb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
System.out.println("class loaded succesfully");
System.out.println("con="+con);
con = DriverManager.getConnection (url,username,password );
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println ("Cannot connect to database server");
}
}
}
output:
class loaded successfully
con=null

i tried all other codes but same result class loading , database is not connecting.
i copied mysql-connector-java-5.1.5-bin.jar file to /../myproject/webroot/web-inf/lib folder... then only i am able to load atleast the class...... i think i have change some setting in eclipse . can anybody suggest me what to do n how to do. please help me out.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2007
Added on Nov 30 2007
8 comments
705 views