Skip to main content

JDBC and MySQL


Here is a quick reference to connect mysql database with java
in linux environment.

INSTALLATION:

Hope jdk, mysql server and client are already installed on your box.
then install the libmysql-java

sudo apt-get install libmysql-java

it will install the connector in your box or download the jar file manually from

http://dev.mysql.com/downloads/connector/j/

if mysql-connector-java.jar is located in the /usr/share/java
then set the classpath

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


to make this CLASSPATH persistent add them to .bashrc file

now check with

echo $CLASSPATH

it will show similar to this :/usr/share/java/mysql-connector-java.jar

MYSQL PART

Create a user with all privileges by doing these things

at the mysql> prompt

USE mysql;

create a user mani with all privileges by

create user 'mani'@'localhost' identified by 'mani';
grant select, insert, update, delete, create, drop, references,
execute on *.* to 'mani'@'mani';


then quit the mysql by

exit;

now login with

mysql -h 127.0.0.1 -u mani -p

it will prompt for the password and enter the password we created. that is same as user

now start to create a table with all the things you needed.

JAVA PART:

database url is jdbc:mysql://localhost/dbname

dbname is the database name we want to connect.
//java program to connect mysql and query the student table
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Student {
 //database url
 static final String DB_URL="jdbc:mysql://localhost/test";
 
 //main method
 public static void main(String[] args) {
  Connection connection = null; //connection manage
  Statement statement = null;
  ResultSet resultSet = null;
  
  try {
  //connecting with db
  connection = DriverManager.getConnection(DB_URL,"mani","mani");
  
  //statement 
  statement = connection.createStatement();
  //execute statement
  resultSet = statement.executeQuery(
   "SELECT name,age,grade FROM student");
 
  //process query results
  ResultSetMetaData metaData = resultSet.getMetaData();
  int numColumns = metaData.getColumnCount();
  System.out.println("Student information table: ");
  
  for(int i=1;i<=numColumns;i++) 
   System.out.printf("%-8s\t",metaData.getColumnName(i));
  System.out.println();
  System.out.println();
  while(resultSet.next())
  {
   for(int i=1;i<=numColumns;i++)
    System.out.printf("%-8s\t",resultSet.getObject(i));
   System.out.println();
  } //end while
  } //end try
  catch(SQLException sqlexception) {
   sqlexception.printStackTrace();
  } //end catch
  
  finally
  {
   try
   {
    resultSet.close();
    statement.close();
    connection.close();
   } catch(Exception exception) {
    exception.printStackTrace();
   } //end catch
  } //end finally
 } //end main
} //end class
that's it happy hacking..

Comments

Popular posts from this blog

ARP/RARP full simulation program

server.c #include "stdio.h" #include "stdlib.h" #include "string.h" #include "sys/types.h" #include "sys/socket.h" #include "arpa/inet.h" #include "netinet/in.h" #define SA struct sockaddr struct IPmac { char ip[100]; char mac[100]; }; int main() { int sockfd,len,i; struct sockaddr_in servaddr; char buff[30],temp[30],ip[30],mac[30];

Configure opendns in ubuntu 12.04

Today i have changed my dns server name to opendns. which is alternative to default dns and it provide more secure, faster browsing experience. What is opendns? OpenDNS is the leading provider of Internet security and DNS services Industry leading malware and botnet protection Award winning Web filtering Faster, reliable, more secure DNS Ultra-reliable, globally distributed cloud network Simple, Smart, Easy How to configure opendns in ubuntu 12.04: open terminal and type sudo nano /etc/resolv.conf enter your password and change these lines   nameserver 208.67.222.222  nameserver 208.67.220.220 to use google public dns use 8.8.8.8, 8.8.4.4 instead of 208.67.222.222, 208.67.220.220 simple isn't..! now see your facebook load faster than before. opendns comes with parental control, so never worry about unwanted websites, and your childrens are safe now. to check your dns changed successfully goto welcome.opendns.com

Given a string, reverse only vowels in it; leaving rest of the string as it is

/* Given a string, reverse only vowels in it; leaving rest of the string as it is. Input : abcdef Output : ebcdaf */ import java.io.*; import java.util.*; public class VowelReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); String vowels = ""; String ans = ""; int arr[] = new int[str.length()]; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u') { vowels+=str.charAt(i); arr[i]=1; } } String revVowels = new StringBuffer(vowels).reverse().toString(); int j=0; for(int i=0;i<str.length();i++) { if(arr[i]!=1) { ans+=str.charAt(i); } else { ans+=revVowels.charAt(j); j++; } } System.out.println(ans); } }