Using JDBC to connect to MySQL from Java Program

By: Rajan Viewed: 1380 times  Printer Friendly Format    

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 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 {

Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while ( {
dbtime = rs.getString(1);
} //end while

} //end try

catch(ClassNotFoundException e) {

catch(SQLException e) {

}  //end main

}  //end class

Most Viewed Articles (in JDBC )

Latest Articles (in JDBC)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks! Can u explain how to use PreparedStatement
View Tutorial          By: Shashank Araokar at 2007-05-07 07:41:54

2. Can you explain when the user input through the ab
View Tutorial          By: mujmil at 2008-02-11 18:46:00

3. this is i got error msg when i run a my program .
View Tutorial          By: anbu at 2009-04-08 02:41:17

4. can u explain about printStackTrace()method.what i
View Tutorial          By: himabindu at 2009-06-10 20:03:14

5. @ anub : This is because you need to add external
View Tutorial          By: Sushil at 2009-09-09 12:24:23

6. @ anub : This is because you need to add external
View Tutorial          By: Sushil at 2009-09-09 12:25:00

7. wen i run i get dis error


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

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

9. It seems like your getConnection doesnt recognize
View Tutorial          By: Partha at 2009-11-28 11:32:53

10. i have written above code but the values are not s
View Tutorial          By: varun at 2010-01-15 18:37:19

11. The above code is not for storing values it return
View Tutorial          By: vinoj at 2010-04-28 15:58:47

12. i have run the there r so many excepti
View Tutorial          By: ayan at 2010-05-26 10:37:51

13. i have run the there r so many excepti
View Tutorial          By: ayan at 2010-05-26 10:39:54

14. where should i paste that mysql-connector-java-5.1
View Tutorial          By: satz at 2010-07-05 00:29:41

15. i can saved when i run the program...
but w

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

16. thank you....
I have a problem.
I my

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

17. i fixed it already...

how can I get

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

18. Hi guys, What is the best way to connect to mysql
View Tutorial          By: veron at 2010-09-11 16:13:25

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

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

21. someone explain how to use PreparedStatements with
View Tutorial          By: Manikprabhu at 2011-03-26 00:04:54

22. how to connect the database in servelet use to sta
View Tutorial          By: sudhir singh tomar at 2011-04-28 03:10:34

23. Thank you very much for your help,
this co

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

24. hi friends,
can i write the same jdbc code

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

25. @mahesh yes you can use this to connect to any db
View Tutorial          By: Rita sen at 2012-01-05 05:18:13

26. How to connect a webpage(which I have created for
View Tutorial          By: sam at 2012-01-24 10:10:52

27. you can see here....


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

28. how to change the connection url??? how to add the
View Tutorial          By: mg at 2012-04-14 14:59:14

29. hola

no funciona este

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

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

31. Perfectly working, and able to connect database.
View Tutorial          By: satyajit at 2012-06-01 20:16:53

32. i got an error msg at runtime of jdbc program with
View Tutorial          By: madhu sudan at 2012-06-17 02:52:53

33. I successfully connect to mysql server using:

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

34. MySQL Connector/J supports only TCP/IP connections
View Tutorial          By: Java Training Institute In Chennai at 2012-09-13 12:37:44

35. this what i get on the servlet web page plz some o
View Tutorial          By: boney dsilva at 2012-09-19 15:37:09

36. package com.anand;

import java.sql.

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

37. "Invalid Password at the jsp bcos RuntimeExce
View Tutorial          By: Ogah I. G at 2013-01-08 16:44:54

38. i connect my program to my sql and create table. a
View Tutorial          By: Santosh Agrawal at 2013-04-08 13:00:56

39. Yes. I have been trying to optimize this code for
View Tutorial          By: Józek Java at 2013-05-14 15:01:13

40. unzip jquery-ui-1.10.3.custom

and c

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

41. This information you provided in the blog that was
View Tutorial          By: JAVA Training in Chennai at 2014-08-27 07:12:36

42. thank you for sharing this...

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

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

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

44. Thanks for sharing such informative article on Jav
View Tutorial          By: Java Training in Chennai at 2015-01-02 07:22:43

45. Java is one of the popular technologies with impro
View Tutorial          By: Java Training at 2015-01-23 10:10:21

46. Thanks for your informative article on Java. It is
View Tutorial          By: Java Training at 2015-02-02 08:34:31

47. i want the database connectivity for telephone bil
View Tutorial          By: rohan at 2015-04-05 05:01:46

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

49. What is the difference between << and <&l
View Tutorial          By: Fita at 2015-12-15 09:53:12

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

51. I appreciate your wordpress web template, exactly
View Tutorial          By: Smithc910 at 2016-10-13 09:18:52

52. how con i connect mysql and java via networking
View Tutorial          By: Siraj Sufiyan at 2016-10-27 15:36:09

53. Very good write-up. I definitely appreciate this w
View Tutorial          By: Devops Training at 2016-12-26 09:10:15

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

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

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

57. I know this web page gives quality dependent artic
View Tutorial          By: pole dance barcelona at 2017-08-01 07:40:55