Saturday, May 22, 2010

java/mysql database connection in eclipse

1 ) download mysql/j connector and extract it in c:/ drive.

2)create a new project in eclipse. Right click it and select Build Path/configure build path.

3) Select add library and the select connectivity driver definition and then select next.

4) select new driver definition : 

5)
select Generic JDBC driver

6) select Jar List from the tab
7)
click on add jar/zip and find out the jar file from the extracted directory.

8)click on property tab and select driver class and then select browse for class and select 
com.mysql.jdbc.Driver:
click ok and finish.


Now create a simple program : 

-------------------------------------------------------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class logincheck {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub
                                Connection con=null;
                                System.out.println("hi");
                                try
                                {
                                System.out.println("hi111");
                                con=DriverManager.getConnection("jdbc:mysql://localhost/store","root", "");
                                System.out.println("successful");
                               
                                Statement statement=con.createStatement();
                                String query="SELECT email,password from user";
                                ResultSet result=statement.executeQuery(query);
                               
                                while(result.next())
                                {
                                                System.out.print(result.getString("email"));
                                                System.out.print(result.getString("password"));
                                }
                                con.close();
                                }
                                catch(Exception e)
                                {
                                       System.out.println(e);         
                                }finally
                                  { con.close();
                                   }
}
}
-------------------------------------------------------------------------------------------------------------




No comments:

Post a Comment