Create Class for Database

Wednesday, March 3, 2010 Posted by Unknown
create a package name as database and create a class in that database DBClass

package database;

/**
*
* @author Jagadeesh
*/
public class DBClass {

}


in this package we will create methods for insert,delete,update

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.UserBean;

/**
*
* @author Jagadeesh
*/
public class DBClass {
public Connection createConnection() throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/userdbase", "root", "root");
return connection;
}
public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException
{
//here we will write code for insert

}
public List getAlldetails()throws SQLException, ClassNotFoundException
{
//here we will write code to get all records from database
}
public UserBean getDetails(int uid)throws SQLException, ClassNotFoundException
{
//here we will write code to get a single record from database
}
public int UpateDetails(int uid)throws SQLException, ClassNotFoundException
{
//here we will write code to update a record
}
}

here
getAlldetails will return more than one user details so we returned list object.
getDetails will return only one user details so one bean object is enough.
here i used mysql fourth driver so no need to create any dsn name.you have to copy mysql-connector.jar files to tomcat commons library.if you dont want to use this you can use odbc jdbc bridge driver

in next post we will insert values into database
Labels:

Post a Comment