Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Connect DataBase with perl

Status
Not open for further replies.

6681510

Programmer
Aug 29, 2007
16
FI
Hi Iam new With Perl i would like to know who to connect a any database with perl and what is the way to create a perl.pm file like when we create a class in java which is like i have a code in java where i create a business class . i want to create a same class in perl

Code:
package Project_Race;


/**
* Author Muhammad Butt
* File Name : Participant
* FileTYpe : Business Class
* Purpose : This java file will be used to store the data of
* participants like name , finnishing time.
*
*/
import java.util.Date;
public class Participant {
/**
* Declaring the variables which are going to be
* use when displaying the data to the jsp
* and it will also store the data coming from a database by
* the help of participantsDAO
*/
int participant_number ;
String Firstname ;
String Lastname ;
Date Finnishing_time;
int place;
/**
* @return the place
*/
public int getPlace() {
return place;
}
/**
* @param place the place to set
*/
public void setPlace(int place) {
this.place = place;
}
/**
* @return the finnishing_time
*/
public Date getFinnishing_time() {
return Finnishing_time;
}
/**
* @param finnishing_time the finnishing_time to set
*/
public void setFinnishing_time(Date finnishing_time) {
Finnishing_time = finnishing_time;
}
/**
* @return the firstname
*/
public String getFirstname() {
return Firstname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
Firstname = firstname;
}
/**
* @return the lastname
*/
public String getLastname() {
return Lastname;
}
/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
Lastname = lastname;
}
/**
* @return the participant_number
*/
public int getParticipant_number() {
return participant_number;
}
/**
* @param participant_number the participant_number to set
*/
public void setParticipant_number(int participant_number) {
this.participant_number = participant_number;
}
/**
* This method will display all the information
* available of participants
*/

public String toString()
{
return (participant_number+" "+Firstname+" "+Lastname+" "+Finnishing_time);
}

}
############################################################

and One more thing i want to know how to create a DatabaseConnection class which connects the database and select or insert if u have some kind of sample class then please Show me .i have one in java .

Code :

package Project_Race;

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

import javax.swing.JOptionPane;

public class ParticipantsDAO {
// this is for using : oracleprivate final String jdbcDriverName = "oracle.jdbc.driver.OracleDriver" ;
private final String jdbcDriverName = "sun.jdbc.odbc.JdbcOdbcDriver" ;
private final String connectionString = "jdbc:eek:dbc:atk84f_DB" ;
private boolean jdbcDriverLoaded = false ;

private Connection c;

/**
* This metho will create a connection every time when this class instance will be created
* method name: doconnection
* @return
* @throws Exception
*/
public Connection doConnection()
throws Exception
{

if(jdbcDriverLoaded ==false)
loadDriver();
try
{ c=DriverManager.getConnection(connectionString);

}catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error-010\nDatabase is not connected","Insert",
JOptionPane.WARNING_MESSAGE);

}
System.out.println("connected");
return c;
}

/**
* This method will fatch the data of all the participants
* and store it in participants business class
* @return
* @throws Exception
* method : getPList
*/
public Vector getPList ()
throws Exception
{
Statement statement ;
ResultSet resultSet ;
String q_text="";
Participant participant;

Vector <Participant> myVector = new Vector <Participant> () ;
try
{
// 2) Create a statement object for executing SQL
statement = doConnection().createStatement () ;

// 3) Execute SQL
q_text = "SELECT PARTICIPANTNUMBER,FIRSTNAME,LASTNAME,FINNISHINGTIME FROM RACE order by LASTNAME";
resultSet = statement.executeQuery (q_text) ;

while (resultSet.next())
{
participant = new Participant();
participant.setParticipant_number(resultSet.getInt("PARTICIPANTNUMBER"));
participant.setFirstname(resultSet.getString ("FIRSTNAME"));
participant.setLastname(resultSet.getString ("LASTNAME"));
participant.setFinnishing_time(resultSet.getTime("FINNISHINGTIME"));
myVector.add (participant) ;
}

resultSet.close () ;
// 4) Close the statement
statement.close () ;

// 5) Close the database connection
doConnection().close () ;

return (myVector) ; // Return the data...
}
catch(SQLException sqle)
{
printSQLException (sqle, q_text) ;
throw sqle ;
}
}//end PLIST

/**This method will fatch the data of all the participants
* who have finnished the race and store it in participants business class
* @return
* @throws Exception
* method : PFList
*/
public Vector getPFList ()
throws Exception
{
Statement statement ;
ResultSet resultSet ;
String q_text="";
Participant participant;

Vector <Participant> myVector = new Vector <Participant> () ;
try
{
// 2) Create a statement object for executing SQL
statement = doConnection().createStatement () ;

// 3) Execute SQL
q_text = "SELECT participantnumber,FirstName,LastName,FinnishingTime FROM RACE where FinnishingTime is not null order by FinnishingTime";
resultSet = statement.executeQuery (q_text) ;
int place = 0;
while (resultSet.next())
{
place++;

participant = new Participant();
participant.setPlace(place);
participant.setParticipant_number(resultSet.getInt("participantnumber"));
participant.setFirstname(resultSet.getString ("FirstName"));
participant.setLastname(resultSet.getString ("LastName"));
participant.setFinnishing_time(resultSet.getTime("FinnishingTime"));
myVector.add (participant);

}


resultSet.close () ;


// 4) Close the statement
statement.close () ;

// 5) Close the database connection
doConnection().close () ;

return (myVector) ; // Return the data...
}
catch(SQLException sqle)
{
printSQLException (sqle, q_text) ;
throw sqle ;
}
}//end PFLIST

/**
* This method will be used for Update Participants database
* and take the data of participants in the form of parameters
* and these paramers will be used to update the database
*/
public void UpdateParticipant(
int participant_number,String Firstname,
String Lastname,
String Finnishing_time)
{
Connection con;
Statement statement;
String sql;
sql = "";
try
{ con = doConnection() ;


statement = con.createStatement();

sql = "INSERT INTO RACE (Participantnumber,Firstname,Lastname,Finnishingtime)values ('"+participant_number+"','"+Firstname+"','"+Lastname+"','"+Finnishing_time+"')";


int query = statement.executeUpdate(sql) ;

if (query ==0)
{
JOptionPane.showMessageDialog(null,"Data is not inserted","Error",
JOptionPane.WARNING_MESSAGE);;
}
else
JOptionPane.showMessageDialog(null,"Data is Inserted","Insert",
JOptionPane.WARNING_MESSAGE);
// 4) Close the statement


statement.close () ;

// 5) Close the database connection
con.close();


}catch(Exception e)
{
String searchError = e.getMessage();
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,searchError,"Insert Error",
JOptionPane.WARNING_MESSAGE);



}


}//UpdateParticipants End

/** ******************************************************************
* loadDriver
******************************************************************** */
public void loadDriver () throws Exception
{
try
{
Class.forName (jdbcDriverName) ; // Load the JDBC driver
jdbcDriverLoaded = true ;
}
catch (Exception e) // Exception handler
{
System.out.println ("********** JDBC ERROR ***********") ;
System.out.println ("Source : ParticipantsDAO.java") ;
System.out.println ("Message: Could not load the JDBC driver " +
jdbcDriverName) ;
throw e ; // Throw the exception again...
}
}

/** ******************************************************************
* printSQLException
******************************************************************** */
private void printSQLException (SQLException sqle, String q_text)
{
System.out.println ("********** SQL ERROR ************") ;
System.out.println ("Source : ParticipantsDAO.java") ;
System.out.println ("SQL text : " + q_text) ;
System.out.println ("Exception: " + sqle) ;
}
}
############################################################

thanks
 
Hi

[ul]
[li]Your question has nothing to do with CGI. Post it in forum219.[/li]
[li]When reposting this, add some sentences about what have you tried so far.[/li]
[li]Enclose the posted code with [tt][ignore]
Code:
[/ignore][/tt] .. [tt][ignore]
[/ignore][/tt] tags.[/li]
[/ul]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top