In this post, we will see how to run jdbc program in terminal or command prompt in Linux. Here we have used mysql database.
Watch following video:
Watch on YouTube: https://www.youtube.com/watch?v=S6wyIKcK8fM
Follow the following steps.
1. You need to create user, database and table in mysql database. You need to grant privileges to the user. For details, check this post http://www.comrevo.com/2015/12/mysql-tutorial-basic-sql-queries.html .
We have to download mysql-connector.jar file. We can download it from the link http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjar.htm. Extract zip file and save it at location /usr/share/java.
To run jdbc program on terminal in Linux, you need to edit .bashrc file. You will get .bashrc file in following location
parag@parag-Inspiron-N4010:~$ gedit /home/parag/.bashrc
Then add following line in .bashrc file:
Save it and restart the system.
3. Write a jdbc program as per your requirement.
Check jdbc program for retrieving data in this post http://www.comrevo.com/2017/07/jdbc-code-to-retrieve-data-from-mysql-database.html
Check jdbc program for inserting data into table in this post http://www.comrevo.com/2017/07/jdbc-code-to-insert-data-into-mysql-database.html
4. Open terminal, go the location where you have saved your program, compile code by using javac and run it by java command.
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac jdbcinsert.java
parag@parag-Inspiron-N4010:~/Desktop/prog$ java jdbcinsert
3101 te pict pune
4301 be gcoe amravati
1201 se vjti mumbai