Your First JDBC Program

This is sample or demo JDBC program which will ensure you that everything is fine and your PC is ready for developing JDBC program. Here I have added a demo program. Just copy and paste into your Java Editor and run program. If everything goes right then you will get correct output.

Demo Program

Output

Connecting database…
Database connected!
Closing the connection.
_

Explanation

To connect and work with database you need 4 ingredients in your hand.

1. Appropriate Driver : for MySQL database it is com.mysql.jdbc.Driver.


2. Server Location : It is your localhost or server IP. jdbc:mysql://localhost/ or jdbc:mysql://127.0.0.1/


3. User Name: A user name to login to server. Username you set when installing mysql. Generally username is root.


4. Password: Password for the user. It is set when you installed mysql. Generally it is blank or whatever you set. In my condition it is root.


Connection

Connection is so easy and DriverManager does all the things automatically. You just need to provide appropriate information to DriverManager and DriverManager will make database connectivity for you.

Summary

In this chapter you learned how to make connection with MySQL using JDBC. Once you set all the environment you are ready to dive deep into Java database programming. In the next few chapter you will learn how to execute SQL Query using JDBC command. The right next chapter is Creating Database using JDBC Command.


Leave a Reply

Your email address will not be published. Required fields are marked *