Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday, 22 January 2013

Mysql java connectivity

hi Friends here are the steps to connect java with mysql database
step 2: on your c: drive find "mysql" directory and copy above connector file in "mysql/lib" dirctory.
step 3: right click on mycomputer->properties->advance-system-setting->Environment_variable
step 4: add new variable and set 
VARIABLE NAME=CLASSPATH
VARIABLE VALUE=;C:/<YOUR DIRECTORYs>/mysql/lib/mysql-connector-java-5.1.2.jar;
step 5: click on ok->ok->ok
step 6: write following code
import java.sql.*;
import java.io.*;
public class connect
{
 /*
 * master-gtu.blogspot.com
 * Pankaj Sharma(8460479175)
 */
 public static void main(String args[])
 {
  try
  {
  Class.forName("com.mysql.jdbc.Driver");
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost/neelkanth","root","");
// here neelkanth is database name and root is username and password is blank
  //System.out.println("Connected to DATABASE");
  Statement stmt = con.createStatement();
  ResultSet rs=stmt.executeQuery("select * from notice");
  while(rs.next())
  {
   System.out.println(rs.getString(3));
  }
  con.close();
  }
  catch(Exception e)
  {
   System.out.println(e.toString());
  }
 }
}
step 7: Wao connectivity success... """""""""Happy Coding"""""""""""

3 comments:


  1. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.


    Java and J2EE Training in Chennai - AmitySoft

    ReplyDelete