connect til mySQL?
Hvordan connecter jeg til en mySQL database i min EntityBean i et J2EE miljø?I eksemplet connecter jeg til en Cloudscape database og indsætter et ID, fornavn & efternavn.
Derefter får jeg retuneret et efternavn.
Men jeg vil have en mySQL database koblet til istedet.
Eksemplet er en modificeret udgave af savingsaccount fra sun:(her er den fulde kode):
remote interface:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface SavingsAccount extends EJBObject
{ public String getEfterNavn()
throws RemoteException;
}
home interface:
import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface SavingsAccountHome extends EJBHome
{ public SavingsAccount create(String id, String forNavn,
String efterNavn)
throws RemoteException, CreateException;
}
Entity bønnen:
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class SavingsAccountBean implements EntityBean {
private String id;
private String forNavn;
private String efterNavn;
private EntityContext context;
private Connection con;
private String dbName = \"java:comp/env/jdbc/SavingsAccountDB\";
public String getEfterNavn()
{ return efterNavn;
}
public String ejbCreate(String id, String forNavn, String efterNavn)
{ try
{ insertRow(id, forNavn, efterNavn);
}
catch (Exception ex)
{ throw new EJBException(\"ejbCreate: \" +
ex.getMessage());
}
this.id = id;
this.forNavn = forNavn;
this.efterNavn = efterNavn;
return id;
}
public void ejbRemove() {
try {
deleteRow(id);
} catch (Exception ex) {
throw new EJBException(\"ejbRemove: \" +
ex.getMessage());
}
}
public void setEntityContext(EntityContext context) {
this.context = context;
try {
makeConnection();
} catch (Exception ex) {
throw new EJBException(\"Unable to connect to database. \" +
ex.getMessage());
}
}
public void unsetEntityContext() {
try {
con.close();
} catch (SQLException ex) {
throw new EJBException(\"unsetEntityContext: \" + ex.getMessage());
}
}
public void ejbActivate() {
id = (String)context.getPrimaryKey();
}
public void ejbPassivate() {
id = null;
}
public void ejbLoad() {
try {
loadRow();
} catch (Exception ex) {
throw new EJBException(\"ejbLoad: \" +
ex.getMessage());
}
}
public void ejbStore() {
try {
storeRow();
} catch (Exception ex) {
throw new EJBException(\"ejbLoad: \" +
ex.getMessage());
}
}
public void ejbPostCreate(String id, String forNavn,
String efetrNavn) { }
/*********************** Database Routines *************************/
private void makeConnection() throws NamingException, SQLException {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
con = ds.getConnection();
}
private void insertRow (String id, String forNavn, String efterNavn)
throws SQLException
{ String insertStatement =
\"insert into Bruger values ( ? , ? , ? )\";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement);
prepStmt.setString(1, id);
prepStmt.setString(2, forNavn);
prepStmt.setString(3, efterNavn);
prepStmt.executeUpdate();
prepStmt.close();
}
private void deleteRow(String id) throws SQLException {
String deleteStatement =
\"delete from Bruger where id = ? \";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);
prepStmt.setString(1, id);
prepStmt.executeUpdate();
prepStmt.close();
}
private boolean selectByPrimaryKey(String primaryKey)
throws SQLException {
String selectStatement =
\"select id \" +
\"from Bruger where id = ? \";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, primaryKey);
ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();
prepStmt.close();
return result;
}
private void loadRow() throws SQLException {
String selectStatement =
\"select forNavn, efterNavn \" +
\"from Bruger where id = ? \";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
prepStmt.setString(1, this.id);
ResultSet rs = prepStmt.executeQuery();
if (rs.next()) {
this.forNavn = rs.getString(1);
this.efterNavn = rs.getString(2);
prepStmt.close();
}
else {
prepStmt.close();
throw new NoSuchEntityException(\"Row for id \" + id +
\" not found in database.\");
}
}
private void storeRow() throws SQLException {
String updateStatement =
\"update Bruger set forNavn = ? ,\" +
\"efterNavn = ? \" +
\"where id = ?\";
PreparedStatement prepStmt =
con.prepareStatement(updateStatement);
prepStmt.setString(1, forNavn);
prepStmt.setString(2, efterNavn);
prepStmt.setString(3, id);
int rowCount = prepStmt.executeUpdate();
prepStmt.close();
if (rowCount == 0) {
throw new EJBException(\"Storing row for id \" + id + \" failed.\");
}
}
}
Clienten:
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class SavingsAccountClient
{
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup(\"java:comp/env/ejb/SimpleSavingsAccount\");
SavingsAccountHome home =
(SavingsAccountHome)PortableRemoteObject.narrow(objref,
SavingsAccountHome.class);
SavingsAccount b = home.create(\"15\", \"per\", \"nielsen\");
String eNavn = b.getEfterNavn();
System.out.println(eNavn);
}
catch (Exception ex) {
System.err.println(\"Caught an exception.\" );
ex.printStackTrace();
}
}
}
