Sunteți pe pagina 1din 25

Linglom.

com
Source of IT knowledge

Site search:

Accessing MySQL on NetBeans using JDBC, Part I: Create a connection


Java, NetBeans December 5th, 2007

Introduction
This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB’s JDBC Driver for MySQL.

I’ll divide into 2 parts:

1. Part I : Create a connection


This part which you’re reading shows about how to establish a connection between NetBeans and MySQL.
2. Part II : Perform SQL Operations
This part show how to perform some basic operations from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to
a database.

Requirements
MySQL Connector/J, licensed under the GPL or a commercial license
from MySQL AB.
NetBeans with JRE (Java Runtime Environment).

Step-by-Step guide
1. Installation
Install NetBeans.
Download MySQL Connector/J, name ‘mysql-connector-java-5.0.6.zip’. (The file name may differs depends on the version if
you’ve downloaded from the Official Site at here.)
Extract the zip file to a folder, you’ll see file ‘mysql-connector-java-5.0.6-bin.jar’ which is the library file that we want. Just
copy the file to the library folder, for example to “C:\Program Files\Java\jdk1.6.0_02\lib” directory.
2. Add JDBC Driver to the project on NetBeans (Add a library).
Next, I create a new Java project on NetBeans named ‘TestMySQL’ and add ‘mysql-connector-java-5.0.6-bin.jar’ that I’ve just
extracted from previous step to the project’s library.
1. Create New Project called TestSQL.

2. In Projects window, right click the project name and select Properties.
3. Project Properties window appears. The Categories on left side, select Libraries. And on right side in Compile tab, click Add
JAR/Folder.

4. New Window appears, browse to the file ‘mysql-connector-java-5.0.6-bin.jar’ and click Open.
5. You’ll see the .jar file was added to the project. Click OK to finish.

Note: You should keep mysql-connector-java-5.0.6-bin.jar in the directory that you won’t delete it (ex. not in temp folder).
May be in the same directory that keep common library files. If you delete the file without delete a link from the project, the
project will show error about missing library.
3. Connect to the database.
Now I’m going to write some code to connect to MySQL database. I have configured MySQL service on localhost.
1. I’m going to use Connection and DriverMapper Classes so I need to import libraries.
import java.sql.*;
2. I’ll connect to MySQL Server on local machine, the mysql database(a default database in MySQL). In main method, add the
following code.

try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost/mysql?" +
"user=root&password=123456";
Connection con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}

The code explanation:

Class.forName(“com.mysql.jdbc.Driver”); means load the MySQL driver.


“jdbc:mysql://localhost/mysql?” + “user=root&password=123456″; is a connection string that tells to connect MySQL on
localhost, select database named ‘mysql’ and user/password for MySQL server.
If you would like to connecto to other database, simply change text ‘mysql’ after ‘localhost/’ to your database name.

3. Compile and run the project. If no error occurs, it means that the connection has established successfully.
Next part, I’ll show to how to perform some basic SQL operations to MySQL.

Share and Enjoy:

Related post
Related posts:

1. Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection Introduction This tutorial show you how to use NetBeans to
connect SQL Server (2000 and 2005) by using Microsoft SQL...
2. Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection This article is one of the series of Accessing MS Access
2007 on NetBeans 6.5 using JDBC. You can see...
3. Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations From Part I, I have only established a connection with local
MySQL. Next I’ll show how to retrieve and modify...
4. Accessing SQL Server on NetBeans using JDBC, Part III: Troubleshooting This post is the last part which gathers common problems along
with solutions about accessing SQL Server using JDBC on...
5. Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction Introduction Here comes again, a tutorial about accessing
database on NetBeans. In 2007, I wrote two tutorials which are accessing...

1. Soleil Farah Says:


December 8th, 2007 at 4:26 am
hi
i’m doiong a basketball scoreboard with it control panel on javabeans so any idea about how to connect a java application to mysql! not
connecting netbeans to mysql . i’m facing some difficulties in solving this problem so would you please provide me help or send me a
useful tutorial about the subject ..
thank you for your time

2. vanessa Says:
December 8th, 2007 at 4:37 am
no idea dear!!!!

3. Muhammad Farooq Says:


December 20th, 2007 at 5:43 pm
Hi,
You above mentioned article is very informative. Especially for learners it is very helpful. Keep up and I am waiting for 2nd part.

4. Engineer Says:
December 31st, 2007 at 7:25 pm
The one who does not thank people, he does not thank the GOD

5. Soleil Farah Says:


January 5th, 2008 at 2:42 am
hey all thank for your help!!!! i did the link according 2 this article and itis working..
i have another question: Does anyone know how can i connect 2 different GUIs, i want 2 link a control panel with it corresponding
basketball scoreboard, i want the data to change on scoreboard each time i change it from the soreboard..
Thank you

6. Soleil Farah Says:


January 5th, 2008 at 6:51 pm

sorry i want the data to change on the scoreboard each time i change it from the control panel.. :):)

7. linglom Says:
January 7th, 2008 at 3:27 pm
I’m not sure that I understand your question correctly. If you have data displayed on more than 1 output, I suggest using Timer to
periodically update your data on each output.

8. Soleil Farah Says:


January 7th, 2008 at 5:42 pm
Thank u Linglom!! the thing is i want to update the scoreboard each time i have an action performed on the control panel. I’m
connecting the control to a database implemented on MySQL and i want the scoreboard to retieve data from the database each time i
modify them from the control. this project is what we call an MVC model( Model View Controller)any IDEAs??????
Thank u again for ur help..

9. Alan Says:
February 14th, 2008 at 12:50 am
Thanks for the example. For some reason I received the message: “SQL Exception: java.sql.SQLException: Access denied for user…”. I
then reformatted the getConnection to be in the form of 3 arguments like this:
String connectionUrl = “jdbc:mysql://localhost/”;
Connection con = DriverManager.getConnection(connectionUrl, “User”, “Password”);
where “User” and “Password” were registered in the MySQL server, and it connected fine. Also, I don’t have a db yet, so I left out the
db name.
Thanks again for taking the time to post your example.

10. linglom Says:


February 15th, 2008 at 10:56 am
To Soleil Farah,
Sorry for late answer. You can implement Timer class to refresh your data on scoreboard on every x seconds.

// create a timer, xxxx is time in millisecond


Timer timer = new Timer(xxxx, new ActionListener( {

public void actionPerformed(ActionEvent evt) {


//refresh your scoreboard
}
}
// start the timer
timer.start();

To Alan,
Thank you for sharing.

11. Hasan Says:


February 18th, 2008 at 6:11 am
hi, that was great. thanks for the steps
iam working on it

12. kuuki Says:


February 27th, 2008 at 4:22 am

hi, thx that’s what i need

13. black Hawk Says:


March 14th, 2008 at 7:24 pm
Thanks a lot for this great blog. Simplified access to Mysql.

14. off Says:


March 23rd, 2008 at 9:42 pm
I went to Driver NetBeans for MS Access

15. razmi Says:


June 4th, 2008 at 8:16 am
How can we hide the database username and password in the source code.

16. linglom Says:


June 5th, 2008 at 8:54 am
If you’are using SQL Authentication method as in my example, you have to store username and password somewhere in the program
maybe in code or external configuration file.

Another way, you can use Windows Authentication method which authenticate by using the current user’s credential (user that execute
the application) so no need to specify username and password in application.

17. Arnaud Says:


June 19th, 2008 at 3:37 am
Hello, I’m a completely new to Java and netbeans, I downloaded the MySQL Connector J 5.1.6 and added the address of the file in my
CLASSPATH `enviroment variables`(I’m using Windows XP) , but Netbeans wasn’t able to load the drivers to connect to MySQL.
Any idea why?
I have followed your example and it works very well. So just would like to know why this is not working with the CLASSPATH method out
of curiosity!
Thank you
Arnaud

18. linglom Says:


June 20th, 2008 at 10:50 pm
Quote from NetBeans 5.5 Doc,
“You have to set an explicit classpath in your build scripts because the IDE ignores your environment’s CLASSPATH variable whenever it
runs Ant”

Reference: http://www.netbeans.org/kb/55/using-netbeans/project_setup.html
on Managing the Classpath in Free-form Projects -> Specifying the Classpath for Project Sources

19. dory Says:


June 23rd, 2008 at 7:55 pm
Thank you so much linglom

20. Arnaud Says:


June 23rd, 2008 at 8:01 pm
Thanks for info linglom

21. Yasir Says:


July 20th, 2008 at 1:37 pm
I had done it already using jdk directly but could not accomplish this without this tutorial. Thanks a lot.

22. Khaled Says:


July 29th, 2008 at 8:40 pm
Thanks a lot PRO for this cool, tutorial.
I have wasted a full day to know how to connect to an MySQL database using Linux UBUNTU, but I didn’t know how. But by reading your
tutorial it made things very simple.

Thanks.

23. sofien Says:


August 15th, 2008 at 4:42 pm
Hello people, just to inform you all, what is in this tutoriel are the basics…because to use a database in java you must includes the jdbc
driver for the corresponding database server in the application classpath.
I’m facing a problem which can’t let me remotely connect to MySQL i-e:I have an application installed on different employee computers
and the database installed on a server. So i have to enable remote connection for all IP addresses to my mysql instance…any idea please?

24. Rani Says:


August 20th, 2008 at 10:37 pm
how would connect a MS ACCESS database using the Desktop Application route in Netbeans.

25. Vicky Says:


September 28th, 2008 at 10:24 pm
hi ,it is really useful ,,thanks a lot,,

26. Sam Says:


October 27th, 2008 at 4:40 pm
Thanks a lot mate,
A super tutorial
I have waisted my time using the other tut (http://www.netbeans.org/kb/55/mysql.html) it works locally, but didn’t work when I tried
to publish it.

cheers, sam

27. Danish Kamran Says:


October 29th, 2008 at 2:03 am
Same as Yasir’s comment.Have done it through JDK but creat problem, when I run it through Netbeans. Now resolved.

Thanks
DK

28. Keheliya Says:


December 21st, 2008 at 4:45 pm
Hey…Thanx Very mch..this is so helpful!!

29. Arijit Chattopadhyay Says:


December 26th, 2008 at 10:38 am
I am using RedHat Enterprise Linux 4, netbeans ide 6.0 and mysql 4.1.7. I have done all the abpve steps to connect my java application
with mysql. Although the drvier is loaded successfully but the login process is unsuccessful but from the erminal it is possible to login
into mysql with that username and password

30. Budhi Says:


December 27th, 2008 at 3:15 pm
It is OK if i used jdbc:mysql://localhost:port/dbname but it always fails when I used jdbc:mysql://ip_address:port/dbname

Why ? It almost 1 week i’m searching for solution, but I can’t get one.

31. arc Says:


January 3rd, 2009 at 4:35 pm
Budhi you have only 3 solutions:
1) use SSL
2) enable remote connections on your host (phpadmin, cpanel)
3) use different environment (eg. php+mysql)

32. gary Says:


January 22nd, 2009 at 5:22 am
great, thanks a million. Just started using netbeans, and the tip about importing the library for postgres was a life saver.

thanks

33. shili Says:


January 23rd, 2009 at 9:12 am
hey it worked..u r a genius

34. T Says:
February 12th, 2009 at 8:28 pm
Best explanation I’ve read so far…thanx!

Netbeans 6.5 provides a very simple way for connecting to a database. Though I can’t seem to locate the connection object to execute
queries on :/
Have you tried it?

35. linglom Says:


February 14th, 2009 at 10:03 pm
Hi, T
I have tried that but I can’t remember much. I think it’s not flexible as coding by self.

36. anju Says:


March 19th, 2009 at 12:00 pm
thanx it worked for me

37. rani Says:


March 23rd, 2009 at 1:14 am
need help with ms access – binding table , radio button , etc.

38. linglom Says:


March 28th, 2009 at 8:02 pm
Hi, Rani
There is a tutorial for connect ms access at netbeans.org (http://wiki.netbeans.org/ConnectingToMsAccessDB).

39. sole Says:


April 27th, 2009 at 4:42 am
hi ya,

cheers for writing this tutorial. it’s very useful and it works just fine. if only i found this blog sooner, spent an entire weekend trying to
connect to my mysql database without success…

cheers again and keep up the good work mate

40. maguzek Says:


April 29th, 2009 at 12:16 am
Thanks!

41. kemal Says:


July 17th, 2009 at 1:19 am

many thanks. after 1 hour you saved my life

42. Thank_you Says:


July 20th, 2009 at 12:01 pm
THANK YOU! It was very helpful.

43. inez Says:


August 2nd, 2009 at 8:27 pm
I tried it and it’s still not working for me =( What should I do next? I’ve tried to copy the .jar file to each lib folder, both in jdk and jre.

44. inez Says:


August 2nd, 2009 at 9:06 pm
OK it was my fault. I typed the driver name “com.jdbc.mysql.Driver” when the right one is “com.mysql.jdbc.Driver”. So your suggestion
works. Thank you very much. =D

45. cha Says:


August 6th, 2009 at 8:33 am
thanks alot, it was killing me

46. Maggie Says:


August 10th, 2009 at 11:29 pm
THANK YOU SO MUCH! I have been missing the “properties add JAR” step and this is the first time I have seen it after 2 days of looking
online for a solution!

47. Rani Says:


August 11th, 2009 at 2:11 am
Hi I am working with MS Access & Netbeans 6.5.1- how do you
a) display all records from the DB to a jTable – and to show changes made to records on the table.
b) how do you add music to a program (Netbeans GUI and java code)
c) how do program a print button – to print records from a MS DB.

Your assistance will be greatly appreciated.

48. Abdul Says:


September 25th, 2009 at 6:19 pm
Thanx

49. manoj Says:


September 30th, 2009 at 8:02 pm
thanx , it is very helpful for beginners

50. Nipuna Says:


October 4th, 2009 at 12:08 pm
im trying to connect using no password. can you please tell me the correct connection url??

51. manoj Says:


October 4th, 2009 at 12:18 pm
String connectionUrl = “jdbc:mysql://localhost/database_name?” +
“user=root&password=”;

i m using same without using ny password.

52. patrick Says:


October 4th, 2009 at 5:07 pm
Hi, I’m trying to connect but failed. I am working with Netbeans 6.7.1 and I’ve get Message: “java.net.ConnectException: Connection
refused : connect”
Please tell me what is my problem

53. linglom Says:


October 6th, 2009 at 1:33 pm
Hi, Patrick
What SQL Server edition you are trying to connect to? If it is SQL Server Express, have you enable remote connection?

54. patrick Says:


October 11th, 2009 at 2:08 am
It was the security problem, now I get SQL Exception

55. Hareesh Says:


October 17th, 2009 at 10:51 am
Hello…linglom,

Using the snippet provided above I’m trying to retreive the contents of the Database table Person. I’ve already created the table using
mysql.
But as soon as I execute this I’m getting the following exception.

” SQL Exception: java.sql.SQLException: Operation not allowed after ResultSet closed ”

The table created

mysql> select *from Person;


+———+—-+
| name | id |
+———+—-+
| xyz | 5 |
+———+—-+

package testapp;
import java.sql.*;

public class Main {

public static void main(String[] args) {


Statement stmt;
ResultSet Results;
String FirstName = null,Id = null,printrow;

try {
Class.forName(“com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:mysql://localhost/mysql?” + “user=root&password=pwdpwd”;
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost/Customer”,”root”,”pwd”);
String query=”Select *from Person”;
stmt=con.createStatement();
Results=stmt.executeQuery(query);
stmt.close();

boolean Records=Results.next();

if(!Records)
{
System.out.println(“No Data returned”);
return;

}
else
{

do{
FirstName=Results.getString(1);
Id=Results.getString(2);
printrow=FirstName+” “+Id;
System.out.println(printrow);

}while(Results.next());
}

} catch (SQLException e) {
System.out.println(“SQL Exception: “+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println(“Class Not Found Exception: “+ cE.toString());
}

56. neeraj Says:


October 17th, 2009 at 1:12 pm
hey thats really helpful

57. linglom Says:


October 19th, 2009 at 9:31 am
Hi, Hareesh
If you are still referencing to the ResultSet object, do not close the Statement object yet. For this example, move the code
“stmt.close();” to the bottom after the do-while loop should fix the problem.

58. pkmnambiar Says:


October 28th, 2009 at 9:28 am
Hi Guys,

I have NetBeans 6.5 IDE on ubuntu 9.04.


Please tell me where to copy-paste jar file.

Thanks in advance!

59. Atheer Says:


October 28th, 2009 at 3:27 pm
Thank you, Thank you, Thank you.

God bless you.

60. linglom Says:


October 30th, 2009 at 9:40 am
Hi, pkmnambiar
You don’t need to place the jar file on java folder. You can place any folder you want because you will browse to the file later by
yourself.

For Linux, the default java path should be /usr/bin/java.

61. ritika Says:


November 3rd, 2009 at 5:11 pm
hi please let me know how to configure the mysql service pls help me

62. shola Says:


November 4th, 2009 at 12:20 am
i create tables of my mysql database using netbeans, but i cant find from where in netbeans can i set autoincrement or autonum
property of a coloumn of my table? further that i couldnt find it so i created the tables and i would like to modify the tables to set
autonum in them instead of making them again so plz help in both matters

63. ritika Says:


November 4th, 2009 at 4:20 pm
i got it..

64. pkmnambiar Says:


November 4th, 2009 at 5:39 pm
@linglom

Thanks dude…..

65. vishnu Says:


December 14th, 2009 at 11:33 am
thnx a looot…
finally i get the answer how to use jdbc…

66. Ankit Says:


January 7th, 2010 at 2:05 am
This information really helped me out.
But as I compile, it says
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
SQL Exception: java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
BUILD SUCCESSFUL (total time: 4 seconds)

It says Build is sccuessful but also says failure handshake. Can you please let me know if the connection is successful or not?

Thanks
Ankit

67. linglom Says:


January 7th, 2010 at 11:20 am
Hi, Ankit
Try to change the authentication method at the MySQL server.

set password for @ = old_password(‘ ‘);

68. Fö a betűm Says:


January 11th, 2010 at 12:16 am
Hey man!
Thanks for this tutorial at last i have found a good one, a step by step one, not a “check the documentation” one.

69. Nissy Says:


January 21st, 2010 at 3:34 am
how do i create and connect a mobile application to mysql database

70. Alvin Says:


January 21st, 2010 at 11:52 pm
hi, I face some problems. Hope can get a solution. Thanks in advance.

package my.test;
import java.sql.*;
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
int a = 5;
a= a^3;
System.out.println(Math.pow(2, 4));
DB db = new DB();
Connection conn=db.dbConnect(
“jdbc:mysql://localhost:3306/applet”, “root”, “password”);
try{
Statement query= conn.createStatement();
query.executeQuery(“select * from tabletest where ID=\’3\’”);
ResultSet rs = query.getResultSet();
System.out.println(rs.next());
int count = 0;
while (rs.next ())
{
int idVal = rs.getInt (“ID”);
int recordX = rs.getInt (“RecordX”);
int recordY = rs.getInt (“RecordY”);
System.out.println (
“id = ” + idVal
+ “, name = ” + recordX
+ “, category = ” + recordY);
++count;
}
rs.close ();
query.close ();
System.out.println (count + ” rows were retrieved”);
List list = new LinkedList();
list.add(1);
list.add(3);
list.add(5);
System.out.println(list.size());
}
catch(Exception e)
{

}
}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,


String db_userid, String db_password)
{
try
{
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

System.out.println(“connected”);
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
};

71. Alvin Says:


January 21st, 2010 at 11:53 pm
output shown at below:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect


at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.(Socket.java:366)
at java.net.Socket.(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at my.test.DB.dbConnect(NewClass.java:63)
at my.test.NewClass.main(NewClass.java:17)

** END NESTED EXCEPTION **

Last packet sent to the server was 0 ms ago.


at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at my.test.DB.dbConnect(NewClass.java:63)
at my.test.NewClass.main(NewClass.java:17)
BUILD SUCCESSFUL (total time: 2 seconds)

72. Nissy Says:


January 22nd, 2010 at 2:59 am
I need step-by-step tutorial on how to create a mobile application using netbeans-mobility and mysql

73. linglom Says:


January 22nd, 2010 at 4:55 pm
Hi, Alvin
It seems that you have 2 problems:

1. Communications link failure due to underlying exception


2. Connection refused: connect
For the first problem, I suggest you follow this thread:
CORRECT SOLUTION: Communications link failure due to underlying exception

For the second problem, try to change the hostname in the connection string to ip address, try both 127.0.0.1 and your network ip
address.

Hi, Nissy
I’m not have experience with mobile application. But I think the code for mobile application should be simialr with this example.

74. Alvin Says:


January 24th, 2010 at 9:51 pm
Hi, linglom
My problem seems hard to be solved.
Anyway, thanks for your help!!!

75. Alvin Says:


January 24th, 2010 at 10:40 pm
bro, my problem has been solved already..
Thank you

76. vipul Says:


January 31st, 2010 at 4:09 am
i got my prob solved through this tutorial….thanx to u

77. Awe Says:


February 1st, 2010 at 6:03 pm
Thanks a lot, this is very explanatory and straight forward. It solved my problem. Thank again…

78. Faisal Yousuf Says:


February 8th, 2010 at 2:08 pm
Thank you very much. I could not connect programmaticly to MySQL even though I could connect through the Services tab. The solution
is to add the JAR file that contains the MySQL Connector. You describes how to do that step. Once I added the JAR file, everything
worked.

Once again, thank you.

79. RabiaN Says:


February 11th, 2010 at 3:36 am
Thnx alot! it works perfect and solved my problem

80. marcos Says:


February 18th, 2010 at 4:49 am
thanks as in 4 real

81. Anish Says:


February 20th, 2010 at 4:02 pm
Thanks Sir,It was of great help for me …….

82. Pallab Says:


February 21st, 2010 at 9:53 pm
Thanks dude …it was a great help

83. steven Says:


February 22nd, 2010 at 8:48 pm
i got this exception while compiling can any one say wat problem !!! java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

84. steven Says:


February 22nd, 2010 at 9:01 pm
pls help me in this exception Have no FileObject for C:\Program Files\Java\jdk1.6.0_13\jre\lib\sunrsasign.jar
Have no FileObject for C:\Program Files\Java\jdk1.6.0_13\jre\classes
SQl EXCEPTIONcom.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect


at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:284)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2569)
at com.mysql.jdbc.Connection.(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at testmysql.Main.main(Main.java:23)

** END NESTED EXCEPTION **

Last packet sent to the server was 32 ms ago.


BUILD SUCCESSFUL (total time: 2 seconds)

85. linglom Says:


February 23rd, 2010 at 11:29 am
Hi, Steven
I suggest you see the comment 73.

86. rahul Says:


March 12th, 2010 at 5:21 am
it was very helpful………

THanks a lot

87. nagarajan Says:


March 14th, 2010 at 7:32 pm
goood and thank you

88. Terrence Says:


March 19th, 2010 at 11:06 am
hi.. i follow the instructions above but I arrive at this output.. What does it mean. help.!Thnx

run:
SQL Exception: java.sql.SQLException: Access denied for user ‘root’@'localhost’ (using password: YES)
BUILD SUCCESSFUL (total time: 0 seconds)

89. linglom Says:


March 22nd, 2010 at 3:31 pm
Hi, Terrence

The error message stated that you had provided with the wrong password for user ‘root’. You need to change user name and password
to the correct one in the connection string.

90. Abhijeet Says:


March 25th, 2010 at 10:34 pm
Hi,

Very Simple and Elegant.


Nice work.
Thanks!

91. mayuran Says:


April 7th, 2010 at 12:31 am
hi,
thanks a lot
it was very helpful…

92. jokki Says:


April 12th, 2010 at 8:34 am
hey,

exclnt marvls superb


keep up..
tnx…

93. Vikash Agarwal Says:


April 13th, 2010 at 3:26 am
Thanks a lot dude.
I am a new user to JDBC and really found this post useful…

94. Tomas Chabada Says:


April 21st, 2010 at 12:43 am
Thanks. Perfect!!!

95. tharaka (Sri Lanka) Says:


April 26th, 2010 at 2:05 pm
excellent, thank you very much.

96. Ninku Reka Says:


April 26th, 2010 at 11:13 pm
good and excellent tutorial , i like it

97. Kumar. S Says:


May 14th, 2010 at 1:09 pm
Sir,

I was terrible upset for I was not able connect to Oracle DB using netbean. Your lesson part one and two has given me breather and I am
so happy that I am able to connect to Oracle. Your style creating a class with a connection method, object creation to get connection is
excellent.

Thank you very much Sir.

God bless you Sir.

S.Kumar

98. Kumar. S Says:


May 14th, 2010 at 1:24 pm
Thank you.

Sir.
A good lesson.

99. manish Says:


June 5th, 2010 at 12:11 am
hey every1 cn u hlp me nd suggest a topic for my proj for the subj “INFORMATICS PRACTICES” m in 12th standard ryt nw…!! rply..!

Thnkx 4 ua concern

100. Rika Says:


June 19th, 2010 at 1:47 pm
Thanks for your info! it’s helpfull.
Anyway may I have this tutorial in a pdf file. Thanks a lot

101. shalini Says:


July 5th, 2010 at 8:28 pm
hey m not understanding dis..can u say how to add dat library on netbeans(jdbc driver)..please m new to dis..can u help me

102. shalini Says:


July 6th, 2010 at 6:45 pm
hey i got how to connect dis..just wanna know where dis programs ll b stored…like in java software r netbeans software..nd where the
exact location

103. linglom Says:


July 15th, 2010 at 10:34 am
Hi, Shalini
I showed how to add a library on this post on step 2 of this post. A project will be stored on the path that you define when you created
it.

104. shalini Says:


July 20th, 2010 at 10:24 pm
hi i just wanna know dat dis mysql need some password to access..i have given the password but after somedays i changed the
password..now m not able to access the mysql…can u help me please……

105. abdullah Says:


July 21st, 2010 at 8:31 pm
@shalini: i think you have to un install the database and create new one

106. Noe Says:


August 11th, 2010 at 5:03 am
Nice, very useful, it works, Thanks a lot

107. lunar Says:


August 17th, 2010 at 4:27 pm
Hi everyone, when i connect to mysql on Netbeans, it’s successful. But when i build to jar file, and i run :
java -jar test.jar
It’s not worked.
Here is my code :
String serverName = HeadNodeIp;
String mydatabase = “hpcosdb”;
String url = “jdbc:mysql://” + serverName + “/” + mydatabase; // a JDBC url
String username = “root”;
String password = “123456″;
try
{
// Load the JDBC driver
System.out.println(“org.gjt.mm.mysql.Driver”);
String driverName = “org.gjt.mm.mysql.Driver”; // MySQL MM JDBC driver
Class.forName(driverName);

System.out.println(“pass class.forName”);
// Create a connection to the database
Connection conn = DriverManager.getConnection(url, username, password);

System.out.println(“pass Connection”);

Error at :
DriverManager.getConnection(url, username, password);

Here is result when i run : java -jar test.jar

org.gjt.mm.mysql.Driver
pass class.forName
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near ‘????????????????’ at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1051)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1856)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3457)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2328)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:774)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:371)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(libgcj.so.90)
at java.sql.DriverManager.getConnection(libgcj.so.90)
at test.DBCommunication.ExecuteQuerry(DBCommunication.java:52)
at test.Main.main(Main.java:22)
SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right
syntax to use near ‘????????????????’ at line 1
SQLState: 42000
VendorError: 1064

Can anybody help me ?

108. lunar Says:


August 17th, 2010 at 9:38 pm
Hi, i found the reason. When i installed Netbean, i set it link to jdk 1.6. But, jdk of my debian is the version 1.5. So, when i built to jar
file and run , it’s not worked in jdk 1.5.
I updated jdk to version 1.6 and it’s worked !

109. saran Says:


August 26th, 2010 at 12:34 pm
thanks a lot……..
i was struggling with this………..
thank u so much

110. subhalaxmi Says:


August 26th, 2010 at 2:35 pm
i m getting a error below dis line
s.updateQuery(“insert into introntb (Accessionid,Organism,Defintion) values (‘”+id+”””+org+”””+def+”‘”);
is dis query writing z correct.
my db is created in mysql when i want to insert d values dis problem arises

111. subhalaxmi Says:


August 26th, 2010 at 2:36 pm
error is below dis id..in d query

112. joyce Says:


August 29th, 2010 at 6:33 pm

heyy thanx a lot u saved my life

113. balamurugan Says:


September 15th, 2010 at 8:26 am
when I compile your code
I get following exception
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

I solution friend

114. balamurugan Says:


September 15th, 2010 at 8:45 am
I have also used following code

Class.forName(“com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:mysql://localhost:3306/balsdb”;
Connection con = DriverManager.getConnection(connectionUrl,”Admin”,”admin”);

but i ‘ve got following exception


ava.sql.SQLException: Access denied for user ‘Admin’@'localhost’ (using password: YES)
can u explain what it means friend

115. linglom Says:


September 15th, 2010 at 9:10 am
Hi, Balamurugan

Verify if that username and password is correct and it has sufficient privilege to access the selected database.

116. Satya Prakash Says:


September 19th, 2010 at 11:00 pm
I am new fool in Java world. I started learning JAva and servlet.
I used mysql using eclipse after installing connector J but the code from netBeans was not working.
I could not understand why I need to add the file in project lib when I have added that jar file in path.

117. Dhruv Patel Says:


September 21st, 2010 at 6:25 am
Hey friends. Thanks for all this information. I am new in Java programming and this information helps me a lot. Thanks again.

118. 6. Java for Web Development for PHP developer – a rant Says:
September 24th, 2010 at 1:26 am
[...] I was getting better error explanation in Eclipse. After searching on Google, I got the solution. Solution is to mention the file in
Library from the project's properties [...]

119. Gashteovski Says:


October 3rd, 2010 at 11:06 pm
Whoaa dude, you’ve saved me a lot of nerves and hours with this. Goes to bookmarks for me!
Cheers!

120. James Umokoro Says:


October 6th, 2010 at 10:43 am
This was really helpful, straight to the point. Thank you.
121. Mridula Says:
October 6th, 2010 at 6:12 pm
Really helpful..too gd,thanx:)

122. Sam Says:


October 13th, 2010 at 9:11 pm
buudy,this is imp. i need a project on on sql and netbeans connectivity all joined in one……

123. Prateek@java Says:


October 28th, 2010 at 3:24 pm
Thanks lunar.. It really worked…

124. deepitha Says:


November 27th, 2010 at 5:06 pm
thanks so much this one is really helpful to me otherwise i had to face lot of problem and thanks again !!

125. sneha Says:


November 29th, 2010 at 10:55 pm
Hi,
my problem is that when i am connecting my to netbeans,there is an occurs in creating ReultSet objects.
code is showing that ResultSet method is not found,it belongs to java.beans.String.
I imported java.beans.*; but the problem is not getting solved.

126. sneha Says:


November 29th, 2010 at 11:14 pm
Hi,
because of my problem all options available with ResultSet object such as executequery and executeupdate are not coming which are
essential for fetching data from database.
pls solve my prblm.

127. swathi Says:


December 7th, 2010 at 6:58 pm
how to store the data in mysqldb using netbeans n the data willbe given by the user

128. beth Says:


December 10th, 2010 at 8:56 am
Hi,

I need help in connecting a database (mysql) to a java web application. Please help by giving me an idea on how to go about it. I don’t
have an idea.

Thanks.

129. Ranjodh Says:


December 18th, 2010 at 5:14 pm
Hi,

Thanks for ur wonderful tutorial. Saved lot of my time.


Thanks again.

130. Richa Says:


December 24th, 2010 at 12:29 am
Hi,
M a newbie trying to connect existing MySQL sample databases through Netbeans 6.9.1. Connections are OK. It also shows the database
(e.g. sakila) on the link:
jdbc:mysql://localhost:3306/sakila [root on default schema]
But the problem is that the Tables folder remains empty.
I have downloaded and re-installed sakila multiple times but problem is not solved. Please help. Thanx.

131. venkat Says:


January 13th, 2011 at 1:18 pm
hi,i m developing a project on hostel management so i need ur help to how to integrate mysql to netbeans

132. venkat Says:


January 13th, 2011 at 2:07 pm
Hi ,
i tried the above method,but i got

SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
BUILD SUCCESSFUL (total time: 9 seconds)

can u please help me in getting through this problem..

133. Sait Says:


January 23rd, 2011 at 2:51 am
Thank you very much for this simple and effective tutorial.
It just works when done step-by-step.
All clear.

134. Byron Says:


January 27th, 2011 at 1:03 am
run:
Class Not Found Exception: java.lang.ClassNotFoundException: com.mysql.jdbc.driver

that is my output… need help… am using Netbeans 6.9.1…

do I still need to download mysql connector.? can’t configure it though… pls post or email me thanks

lordbyron_yumul@yahoo.com

135. Harshit Says:


January 28th, 2011 at 5:07 pm
hey i am using Netbeans Ide 6.5 and can connect mysql database through services but finding problem in program it always shows the
Exception statement i.e. Error in Conectivity
.can anyone help me out.?

136. UDOYE SAMUEL Says:


January 31st, 2011 at 2:35 pm
hi thanks for the tutorial it really helped me,i was developing an application and got hooked up,but with your tutorial on mysqljdbc-
connector 5.0,wow i feel so relieved thanks very much

137. pradip garala Says:


February 4th, 2011 at 3:24 pm
Thanks buddy……..

138. PrathapSingh Says:


February 4th, 2011 at 4:56 pm

Thanks

139. prem Says:


February 9th, 2011 at 12:44 am

Thanks a lot very easy to do connectivity.


140. PrathapSingh Says:
February 9th, 2011 at 6:58 pm
can u send working procedures with struts 1&2

141. linglom Says:


February 21st, 2011 at 10:52 am
Hi, Byron

Have you add MySQL Connector/J library to your project and “import java.sql.*;” in your code?

Hi, Harshit

Can you show the full error message?

142. Monika Says:


February 23rd, 2011 at 10:15 pm
thank u sir… it helps me alot

143. Monika Says:


February 23rd, 2011 at 10:16 pm
thank u sir… it helps me alot …:)

144. Malikge Says:


March 6th, 2011 at 1:43 pm
Thanks Brother…

145. Lawrance Says:


March 7th, 2011 at 4:27 pm
It helped me a lot. Thanks!

146. sana Says:


March 7th, 2011 at 11:53 pm

phew! thanks a lot!..

Subscribe without commenting


E-Mail:

S-ar putea să vă placă și