Coding Lab - TechOnTechnology

How to Connect Database in Java Programming :




Steps:

1. First You need to open database in order to use DB in your program.
 
Open terminal and type

mysql -u root -p
 

and enter password

You will get mysql Prompt.

mysql>

2. Use Following commands.


show databases   = to show the complete set of databases [ individual contains no of tables ]

use databasename =
to select the particular database
 
After this we can use this database in program and to create tables

show tables =
to see the number of tables in it

3.if there is no table,then create a table using following commands.

create table tbname ( field1,fileld2,... fieldn)
=which willl create an table with table name tbname

=where each field= type name

ex:
int reg
varchar(10) name.. etc

4. to insert a data into the table

insert into tbname values(values for that number of fields)

values like.

ex:
7
8
9
'TechOnTechnology'

Note : String will be given using the single quote[  ' ]

5. to view the table

select * from tbname


6. then to run a java program

create a java file
type your program
then save

7. then set class path
this to get proper connection between database and the program using mysql connector.


syntax:


export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.10.jar


Note :  which sets the class path as the driver folder
             that driver name may be changes depend on version numbers, go to that folder and verify

8. to see the class path


echo CLASSPATH
or
echo $CLASSPATH

which will display the classpath


9.then compile the programm

 javac abc.java

abc is the name of the java program file.
 
10.then run 

java abc

Steps to connect this database in program. Please refer program we provided.








Name

Email *

Message *