Results 1 to 19 of 19
Thread: Connection with MySQL
- 07-26-2011, 11:21 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Connection with MySQL
Hello Friends.
I have been working with Java and MS-SQL Server 2005. Now i want to connect to MySQL.
I have downloaded JConnector driver for MySAL but dont know how to install it.
I read the instructions on MySQL website but still could'nt installed it.
So can anyone please explain me.
Any help will be highly appreciated!
- 07-26-2011, 11:52 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You don't install it.
You include it as part of your app.
- 07-26-2011, 12:39 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Do you know what you did in order to use the SQL Server jdbc driver? Do the same for the MySQL driver.
- 07-26-2011, 01:41 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
What i did with MS-SQL is install it and then create a DSN for my database using ODBC. But i don't understand how to configure JConnector so that i can create a DSN!
- 07-26-2011, 01:51 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You don't use ODBC for this.
The docs for JConnector will tell you what the connection syntax is for your getConnection call.
- 07-26-2011, 01:53 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
- 07-26-2011, 07:16 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Its ok. ThaNK YOU FOR YOUR HELP.
ATLEAST I M LEARNING SOMETHING.
- 09-19-2011, 07:28 AM #8
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
What u need to do is you have to download the mysql connector.jar and place that jar file in your lib folder of jdk. then you need to copy the path of the jar that will be like c://program files/java/jdk1.5/lib/mysqlconnect.jar and paste it beside the classpath of ur jdk by using semicolon in the environment variables. then when you write your jdbc code you have specify the location of ur database server by declaring a string url and giving it a value jdbc:mysql://localhost:3306/yourdatabasename. and then when you open the connection pass the same url followed by the username and password as parameters to the DriverManager.getConnection method.
- 09-19-2011, 10:39 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Connection with MySQL
No...that won't work, as they are using SQL Server.
No, you should never put extra stuff in your JDK lib folder.
No, that's not the recommended way of handling a Java apps classpath. You place your driver jar file in with your other libs associated with your project. That way you'll have no problems if one project needs an upgraded jar file, but another doesn't.
This is possibly the only correct thing, albeit for the wrong database.
- 09-19-2011, 11:39 AM #10
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
[QUOTE=Tolls;234906
No, you should never put extra stuff in your JDK lib folder.
No, that's not the recommended way of handling a Java apps classpath. You place your driver jar file in with your other libs associated with your project. That way you'll have no problems if one project needs an upgraded jar file, but another doesn't.
But what if i want to run the jdbc independently. In clear words, like i have no projects and yesterday i tried to run one simple jdbc program for my mysql database and i got this suitable driver not found error for which am still working on it to resolve it. What i mean to ask is that suppose i write a simple jdbc program and try to run that program through command prompt shouldn't for that case the jar file should be placed in jdk/lib folder???
Thank you.
- 09-19-2011, 12:05 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Connection with MySQL
You can't run it independently.
JDBC is a framework for building apps, not an app in itself.
And if you are building an app, then you need to give the app any librarise it depends on.
When running the app, you supply a classpath (usually using -cp) to those libraries.
- 09-19-2011, 02:02 PM #12
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
okay so you mean to say that i need to mix the jdbc with my webapp even if i need to test my jdbc connection and paste the mysql connector jar file to the lib directory of my web project and set the classpath accordingly???
Thank you.
- 09-19-2011, 02:25 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Connection with MySQL
I don't know how you are writing your apps, but the jar file would be part of your WAR that would be dropped into your server.
Most servers pick up the jar files contained in the WEB-INF/lib directory as part of the classpath fro a web-app.
As for "mix the jdbc in", it is (presumably) part of your web app. If it is intended for multiple apps then write it as a seperate module and test that.
- 09-19-2011, 04:23 PM #14
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
No actually since this is my first stint with jdbc what i was trying to do was write code in note pad, copy the jar file to jdk/lib because this is how i came across in some web site and then run the program using the command line by javac and my filename and so on. Though i have tomcat installed i had no intention of that.
But anyways thanks for the enlightenment, tonight i will try by your method.
Thank you once again.
- 09-19-2011, 04:34 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Connection with MySQL
Then you ought to have the libraries you need in a directory along with the code you're working on:
And, running from MyAppDirectory on the command line:Java Code:MyAppDirectory -> lib (contains any jars you need) mytoppackage -> etc etc.
java -cp .;lib/*.jar mytoppackage/MyProgramClass
- 09-20-2011, 03:45 AM #16
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
Actually i got it!!! The thing that i learned was that it is all about setting the class path right!!! What exactly had happened was i had created a classpath variable and assigned it the java path followed by my mysqlconnector.jar path in the environment variables.
But after some time i found out that there was already a variable named path defined in the enivronment variables. so i removed my custom classpath and assigned both the java classpath and mysql connector.jar path to the path variable and it is working....:)
So its about setting your path straight.
Thank you any ways for the assistance.
- 09-20-2011, 09:45 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Connection with MySQL
Don't use environment variables either.
Your application classpath is specific to your application, not your machine as a whole.
It's a bad habit to get into to put this stuff onto your system (or user) classpath.
- 09-20-2011, 03:52 PM #18
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Re: Connection with MySQL
Thanks you
- 09-20-2011, 04:37 PM #19
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Re: Connection with MySQL
hi my friend as you say you have worked with ms sql server 2005
please help me to make connection with the same i m trying this for almost 2 days
my code is
and my error isClass.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
// Connection con = DriverManager.getConnection("jdbc:odbc:my");
String connectionUrl = "jdbc:sqlserver://localhost" +
"databaseName=emp" + "user=sa" + "password=password@123";
// Connection con = DriverManager.getConnection("jdbc:odbc:my//localhost:5000" , "sa" , "password@123");
Connection con = DriverManager.getConnection(connectionUrl);
Statement stat = (Statement) con.createStatement();
// String str = "select * from emp";
ResultSet rs = stat.executeQuery("select * from emp");
i have enabled the tcp ip in the configuration tool and set the tcp port to 1433 under ip address tabrun:
20 Sep, 2011 8:09:28 PM databasedemo.DataBaseDemo main
SEVERE: null
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhostdatabaseName=empuser=sapassword=password@ 123, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.ma keFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.c onnectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.l ogin(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.c onnect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.conne ct(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager .java:582)
at java.sql.DriverManager.getConnection(DriverManager .java:207)
at databasedemo.DataBaseDemo.main(DataBaseDemo.java:3 6)
BUILD SUCCESSFUL (total time: 15 seconds)
please tell me what else i can do or where i m going wrong please i wanted to complete my jdbc project till tommorow
Similar Threads
-
connection my mysql in jsp in myeclipse
By pranjul137 in forum New To JavaReplies: 8Last Post: 04-12-2010, 09:47 AM -
connection my mysql
By pranjul137 in forum New To JavaReplies: 5Last Post: 04-06-2010, 08:26 PM -
TWO IPs FOR MY MYSQL CONNECTION
By tugalsan in forum JDBCReplies: 2Last Post: 03-21-2010, 08:46 PM -
Java-mysql connection
By Kligham in forum New To JavaReplies: 16Last Post: 11-25-2009, 01:28 PM -
no mysql connection
By scchia in forum New To JavaReplies: 12Last Post: 07-19-2008, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks