Using JDBC to connect to MySQL from Java Program

By: Rajan  

This sample Java program connects to MySQL database using JDBC, executes a query and retrieves and prints the value of the database field.

This same sample code can be used to connect to any type of database, all you need to do is change the connection url (dbUrl in the sample). For this code to work properly, you will need to download the mysql driver for JDBC in other words Java Connectors from mysql.com site.

If after downloading the URL it still doesn't work then it is probably due to the classpath. You will have to add the driver jar file in the classpath.

import java.sql.*;
import javax.sql.*;

public class jdbcdemo{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

}  //end main

}  //end class



Archived Comments

1. I know this web page gives quality dependent articles
and other stuff, iis there any other w

View Tutorial          By: pole dance barcelona at 2017-08-01 07:40:55

2. PellRigo
View Tutorial          By: PellRigo at 2017-05-18 04:57:25

3. gregorysm69
View Tutorial          By: walteryz3 at 2017-04-07 21:51:29

4. irisvf4
View Tutorial          By: deirdreep69 at 2017-01-19 04:42:47

5. Very good write-up. I definitely appreciate this website. Continue the good work!
View Tutorial          By: Devops Training at 2016-12-26 09:10:15

6. how con i connect mysql and java via networking
i develop java billing software fine

View Tutorial          By: Siraj Sufiyan at 2016-10-27 15:36:09

7. I appreciate your wordpress web template, exactly where did you down load it through? adakcfkkgcefcc
View Tutorial          By: Smithc910 at 2016-10-13 09:18:52

8. Good post
View Tutorial          By: Selenium Training at 2016-09-13 05:55:47

9. What is the difference between << and <<< operators in java?
View Tutorial          By: Fita at 2015-12-15 09:53:12

10. here in my pc worked, thank you!
View Tutorial          By: Programação at 2015-11-28 13:50:05

11. i want the database connectivity for telephone billing system project can you help me please
View Tutorial          By: rohan at 2015-04-05 05:01:46

12. Thanks for your informative article on Java. It is most popular and reliable software development pl
View Tutorial          By: Java Training at 2015-02-02 08:34:31

13. Java is one of the popular technologies with improved job opportunity for hopeful professionals. <
View Tutorial          By: Java Training at 2015-01-23 10:10:21

14. Thanks for sharing such informative article on Java technology. It is most preferred technology amon
View Tutorial          By: Java Training in Chennai at 2015-01-02 07:22:43

15. I have writtent this code in my jdbc program,

String url = "jdbc:mysql://localh

View Tutorial          By: yeshdeep kumar at 2014-11-12 07:46:46

16. thank you for sharing this...
www.7eleventech.com

View Tutorial          By: oracle training chennai at 2014-09-24 07:30:00

17. This information you provided in the blog that was really unique I love it!!, Thanks for sharing suc
View Tutorial          By: JAVA Training in Chennai at 2014-08-27 07:12:36

18. unzip jquery-ui-1.10.3.custom

and copy mysql-connector-java-5.1.25-bin.jar to your ho

View Tutorial          By: ajmal at 2013-06-06 10:44:55

19. Yes. I have been trying to optimize this code for several hours and I found this solution! Check thi
View Tutorial          By: Józek Java at 2013-05-14 15:01:13

20. i connect my program to my sql and create table. and data are loded , but connection not stabliush!!
View Tutorial          By: Santosh Agrawal at 2013-04-08 13:00:56

21. "Invalid Password at the jsp bcos RuntimeException" How do i solve this error?
View Tutorial          By: Ogah I. G at 2013-01-08 16:44:54

22. package com.anand;

import java.sql.Connection;
import java.sql.DriverManager;<

View Tutorial          By: Anand at 2012-11-12 00:00:07

23. this what i get on the servlet web page plz some one guide me .....-->com.mysql.jdbc.Driver
View Tutorial          By: boney dsilva at 2012-09-19 15:37:09

24. MySQL Connector/J supports only TCP/IP connections
Java does not support Unix domain sockets

View Tutorial          By: Java Training Institute In Chennai at 2012-09-13 12:37:44

25. I successfully connect to mysql server using:
"jdbc:mysql://localhost:3306/mysql";

View Tutorial          By: Arsalan at 2012-07-09 16:38:49

26. i got an error msg at runtime of jdbc program with mysql...
java.lang.ClassNotFoundException:

View Tutorial          By: madhu sudan at 2012-06-17 02:52:53

27. Perfectly working, and able to connect database.
But is it the most efficient way to connect

View Tutorial          By: satyajit at 2012-06-01 20:16:53

28. <b>nice!</b> <img />
View Tutorial          By: Thorsten at 2012-05-27 19:37:28

29. hola

no funciona este
String userName = "userxxxx";

View Tutorial          By: ahmed at 2012-05-11 14:18:46

30. how to change the connection url??? how to add the driver jar file in the classpath??
View Tutorial          By: mg at 2012-04-14 14:59:14

31. you can see here....

http://helloworldcodes.blogspot.com/2012/03/using-mysql-server-i

View Tutorial          By: MIA at 2012-03-21 21:13:45

32. How to connect a webpage(which I have created for my project) to the database(database may be mysql
View Tutorial          By: sam at 2012-01-24 10:10:52

33. @mahesh yes you can use this to connect to any db server. Just get the ip address of the db server a
View Tutorial          By: Rita sen at 2012-01-05 05:18:13

34. hi friends,
can i write the same jdbc code to access the db s/w that is installed in remote c

View Tutorial          By: mahesh at 2012-01-05 05:09:38

35. Thank you very much for your help,
this code is perfect! this has been a great help to me a

View Tutorial          By: AA at 2011-09-09 14:24:45

36. how to connect the database in servelet use to statements
and how to use servlete in ecclips

View Tutorial          By: sudhir singh tomar at 2011-04-28 03:10:34

37. someone explain how to use PreparedStatements with Parameters under MySQL?
View Tutorial          By: Manikprabhu at 2011-03-26 00:04:54

38. Works perfectly...
View Tutorial          By: Prajeesh at 2010-11-26 09:57:52

39. ppookk
View Tutorial          By: pp at 2010-09-21 07:44:36

40. Hi guys, What is the best way to connect to mysql from java? May I connect to mysql in java from ano
View Tutorial          By: veron at 2010-09-11 16:13:25

41. i fixed it already...

how can I get the data from the mysql?!
the record that

View Tutorial          By: kathleen ashley at 2010-08-08 04:51:26

42. thank you....
I have a problem.
I my source code I have a Book as a class and BookEntr

View Tutorial          By: AbhiRules at 2010-08-05 20:55:27

43. i can saved when i run the program...
but when I check the mysql,
there is no record..

View Tutorial          By: kathleen ashley at 2010-08-02 05:22:46

44. where should i paste that mysql-connector-java-5.1.8-bin.jar file?
View Tutorial          By: satz at 2010-07-05 00:29:41

45. i have run the program..bt there r so many exception comes

java.lang.ClassNotFoundExc

View Tutorial          By: ayan at 2010-05-26 10:39:54

46. i have run the program..bt there r so many exception comes

java.lang.ClassNotFoundExc

View Tutorial          By: ayan at 2010-05-26 10:37:51

47. The above code is not for storing values it returns resultset note that sql query is SELECT not INSE
View Tutorial          By: vinoj at 2010-04-28 15:58:47

48. i have written above code but the values are not storing in the database but there is no errors
View Tutorial          By: varun at 2010-01-15 18:37:19

49. It seems like your getConnection doesnt recognize the credentials you pass. Can you try like this?
View Tutorial          By: Partha at 2009-11-28 11:32:53

50. when i tried this code i couldnt run the program...
View Tutorial          By: kavitha at 2009-11-28 01:38:55

51. wen i run i get dis error

jdbc:mysql://localhost:3306/details,root,root
java.s

View Tutorial          By: sandeep at 2009-11-24 08:24:20

52. @ anub : This is because you need to add external jar file that is connector mysql-connector-java-5.
View Tutorial          By: Sushil at 2009-09-09 12:25:00

53. @ anub : This is because you need to add external jar file that is connector mysql-connector-java-5.
View Tutorial          By: Sushil at 2009-09-09 12:24:23

54. can u explain about printStackTrace()method.what is the use of it?
View Tutorial          By: himabindu at 2009-06-10 20:03:14

55. this is i got error msg when i run a my program . how to overcome any one pls helpme. java.lang.Clas
View Tutorial          By: anbu at 2009-04-08 02:41:17

56. Can you explain when the user input through the above code; How can add using a variable parsing?
View Tutorial          By: mujmil at 2008-02-11 18:46:00

57. Thanks! Can u explain how to use PreparedStatements with Parameters under MySQL?
View Tutorial          By: Shashank Araokar at 2007-05-07 07:41:54


Most Viewed Articles (in JDBC )

Latest Articles (in JDBC)

Comment on this tutorial