The Best and Single JDBC example which includes getting input from user and storing it into database right from eclipse. I hope it almost solves all of your doubts ...
A Full JDBC example problem which takes some input from user and saves that into database from eclipse
This post is gonna say you how to take input from user just using Scanner and save that into database directly with proper comments. It is advised to learn core Java before coming to JDBC directly.
I would suggest you to go through very basics of JDBC like what is JDBC and its usage as I m not gonna include that theoretical basics here...
First of all I have tried to make you understand everything piece by piece like how to connect to database and how to use resultset and createstatement methods. Don't worry even if you don't understand it at the beginning I have tried my level best to make you understand everything at the end
How to connect to SQL database (here is the code);
The below mentioned way is simple which is done by using class.forname method, here you can remove try catch if you don't need it
The green highlighted words are credentials which will change system to system.
Then below comes the second way to connect to database
Then we can see the particular code which we use for creating CreateStatement method and to know how to iterate using resultset
Now let's get into the full example
Example problem 1
Following is how we can establish a connection b/w Jdbc and database
In most the JDBC problem which we solve, the below code will be the same as it establishes the connection
2) Following class will be like a DTO which contains getters, setters and constructors
I think I don't have to explain about those basics
3) This
class is a core class which does the main work. DAO
4) Now
let's see what happens in main class
So here we are at the end of the first basic JDBC example I am gonna give you the next example which takes input from user updates that particular row ...if in case that row is not found then a return some error messages or we can throw exception based on your need.
Example Problem 2
The below class is DAO class:
So here our blog comes to the end. I can also share you these codes in text format so that you can directly copy paste and test.
I hope I made it very clear to understand (only if you know java basics and JDBC basics).
Comments
Post a Comment
Please feel free to ask your doubts via comments here