954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java database connectivityl

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

saifjob40
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Where is your database? MYSQL,access? Are you using a particular IDE?

ceyesuma
Posting Pro
524 posts since Aug 2007
Reputation Points: 7
Solved Threads: 2
 

You can find this tutorial helpful http://java.sun.com/javaee/5/docs/tutorial/doc/

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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=""/>
</jsp:useBean>

I had to download a driver fo SQL CONNETOR
mysql-connector-java-5.0.5
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){
		this.dbdriver=dbdriver;
	}
	public void setDbusername(String dbusername){
		this.dbpassword=dbpassword;
	}
	public static Connection getConnection(){
		if(connection==null){
			try{
				Class.forName(dbdriver);
				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");
    sb.append("<tr>");
    for(int i=1; i<=num;i++){
      sb.append("<th>" + rsmd.getColumnName(i) + "</th>\n");
    }
    sb.append("</tr>\n");
    while(rst.next()){
      sb.append("<tr>\n");
      for(int i=1; i<=num; i++){
        sb.append("<td>"+rst.getString(i)+"&nbsp;</td>\n");
      }
      sb.append("</tr>\n");
    }
    sb.append("</table>\n");
    rst.close();
    stm.close();
    return sb.toString();
  }
}


Hope this helps. good luck!

ceyesuma
Posting Pro
524 posts since Aug 2007
Reputation Points: 7
Solved Threads: 2
 

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
Class.forName(DRIVER).newInstance();
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]);
cells[col].add(cell);
}
}

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

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

// exit more gently
catch(Exception e) {
e.printStackTrace();
}
}
}

Ranjeet Kumar
Newbie Poster
3 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You