Saturday, 18 July 2015

Connecting to data base(MYSQL) using JAVA/PYTHON

In this tutorial im going  to discuss how to connect to database(MYSQL) usu= using JAVA and PYTHON

VERSIONS:
      JAVA 1.7
      MYSQL 5
      PYTHON 3.4

connecting to a database

1)JAVA

FIRST WE HAVE ADD MYQL CONNECTOR JAR TO THE PROJECT

RIGHT CLICK ON THE PROJECT -> SELECT PROPERTIES ->SELECT BUILDPATH ->ADD EXTERNAL LIBRARIES ->




Now we can start our coding
        now import mysql in to the class
import java.mysql.*;

make a connection with the database

Connection conn = DriverManager.getConnection("dbname","uername" , "password");

and using the connection create a statement

Statement  stmt = conn.createStatement();

now we can write a query 
for creating a  TABLE



now lets go to the database to see if  table is created


so we have successfully created  created database in java

now lets create a table in python

i have been in java for some years working full time as java devoleper but been smitten with python for some months

we can discuss briefly about python how it differs from java in future post in this post we will just see how to connect to a database using python

to connect python with MySQL we need mysqldriver
 in java to add third party libraries/JAR we simply download them and put it in the JAR folder  here in python we have to install it through pip or easy install now lets install mysql in python

in cm prompt go to the folder were python is installed


now install mysql using the following command


ok lets go to the  code

import mysql.connector
 
conn = mysql.connector.connect(user="root",passwd="",host="localhost",db="solomon",port="3306")
 
 
now we have established a connection with MySQL 



now we have created a table in python .lets see if it exists




 
 so database is created now Lets add some values in to this table




now lets view the result in db



No comments:

Post a Comment