i am new learner in java and i wan to learn
how to do java database connectivity with MySql

i wrote the code still i face problem ,

will any ony plase let me know the wexxat coding for that .

plase help me i am in a greate need

control panel /Administrative Tools/data Sources / systemDSN /"ADD"/MICROSOFT aCCESS dRIVER/FIND your database finisssssssh

<%-- create a bean in application scope, so the bean is shared in the whole Web application --%>
<jsp:useBean id="dbconnection" class="finalWebsitePackage.DBConnection" scope="application"/>
<jsp:setProperty name="dbconnection" property="dbdriver" value="sun.jdbc.odbc.JdbcOdbcDriver"/> //this is for access
<jsp:setProperty name="dbconnection" property="connURL" value="jdbc:odbc:youdatabaseDSN"/>
<jsp:setProperty name="dbconnection" property="dbusername" value=""/>
<jsp:setProperty name="dbconnection" property="dbpassword" value=""/>

I had to download a driver fo SQL CONNETOR
which I think is in the
mysql-noinstall-5.0.41-win32.zip file I down loaded
this stuff is not everything you need but bits and pieces may help
here is a bean

package finalWebsitePackage;
import java.sql.*;

public class DBConnection {
	static String dbdriver;
	static String connURL;
	static String dbusername="";
	static String dbpassword="";	
	static Connection connection=null;
	public String getDbdriver(){
		return dbdriver;		
	public String getConnURL(){
		return connURL;
	public String getDbusername(){
		return dbusername;
	public String getDbpassword(){
		return dbpassword;
	public void setDbdriver(String url){
	public void setDbusername(String dbusername){
	public static Connection getConnection(){
				connection=DriverManager.getConnection(connURL, dbusername,dbpassword);
			}catch(Exception exc){ connection=null;}
		}	return connection;	
package connectionBean.website;
import java.sql.*;
public class DBBean{
  private Statement stm = null;
  private ResultSet rst = null;
  private ResultSetMetaData rsmd =null;
  private String tableName;
  public String getTableName(){ return tableName;}
  public void setTableName(String tableName){
    this.tableName = tableName;}
  public String getData() throws SQLException{
    if(tableName==null || tableName.equals("")) return "";
    Connection conn= DBConnection.getConnection();
    Statement stm= conn.createStatement();
    ResultSet rst= stm.executeQuery("select * from " + tableName);
    ResultSetMetaData rsmd= rst.getMetaData();
    int num=rsmd.getColumnCount();
    StringBuffer sb = new StringBuffer();
    sb.append("<table border=1>\n");
    sb.append("<tr><th colspan="+num+">Records in Table: ");
    sb.append(tableName + "</th></tr>\n");
    for(int i=1; i<=num;i++){
      sb.append("<th>" + rsmd.getColumnName(i) + "</th>\n");
      for(int i=1; i<=num; i++){
    return sb.toString();

Hope this helps. good luck!

It's must halp you.......

import java.sql.*;
import java.util.*;

public class DatabaseTableViewer {
   private static final String DB = "contacts",
                               TABLE_NAME = "records",
                               HOST = "jdbc:mysql://db_host:3306/",
                               ACCOUNT = "account",
                               PASSWORD = "nevermind",
                               DRIVER = "org.gjt.mm.mysql.Driver";

   public static void main (String[] args) {
      try {

         // authentication properties
         Properties props = new Properties();
         props.setProperty("user", ACCOUNT);
         props.setProperty("password", PASSWORD);

         // load driver and prepare to access
         Connection con = DriverManager.getConnection(
            HOST + DB, props);
         Statement stmt = con.createStatement();

         // execute select query
         String query = "SELECT * FROM " + TABLE_NAME + ";";
         ResultSet table = stmt.executeQuery(query);

         // determine properties of table
         ResultSetMetaData meta = table.getMetaData();
         String[] colNames = new String[meta.getColumnCount()];
         Vector[] cells = new Vector[colNames.length];
         for( int col = 0; col < colNames.length; col++) {
            colNames[col] = meta.getColumnName(col + 1);
            cells[col] = new Vector();

         // hold data from result set
         while(table.next()) {
            for(int col = 0; col < colNames.length; col++) {
               Object cell = table.getObject(colNames[col]);

         // print column headings
         for(int col = 0; col < colNames.length; col++)
            System.out.print(colNames[col].toUpperCase() + "\t");

         // print data row-wise
         while(!cells[0].isEmpty()) {
            for(int col = 0; col < colNames.length; col++) 
                                   + "\t");

      // exit more gently
      catch(Exception e) {

Edited 3 Years Ago by happygeek: fixed formatting

Please don't mind the examples above, study on your own speed. Right now this examples can be realy confusing a specialy because they do not provide explanation what they do...

This article has been dead for over six months. Start a new discussion instead.