Archive for March, 2006
JSP Code: Updating records from the MySQL database
The Java code below shows how to update existing records from the database in JSP. Again, this code snippet should be included in the Java class used to connect or disconnect to the database. If you have some questions, feel free to send me an email.
public ResultSet updateFields(String entry1, String entry2, String entry3,
String entry4, String entry5)
throws SQLException, Exception {
ResultSet rs = null;
try {
PreparedStatement pst;
pst = con.prepareStatement("UPDATE academicprofile SET Field1=?,"
+"Field2=?, Field3=?, Field4=? where Field5=?;");
pst.setString(1, Field1);
pst.setString(2, Field2);
pst.setString(3, Field3);
pst.setString(4, Field4);
pst.setString(5, Field5);
pst.executeUpdate();
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
}
return rs;
}
JSP Code: Retrieving records from the MySQL database
The Java code below shows how to retrieve records from the database in JSP. This code snippet should be included in the Java class used to connect or disconnect to the database.
// class file {
// connect(){}
//disconnect(){}
public ResultSet displayRecords() throws SQLException, Exception {
ResultSet rs = null;
String charCompare = null;
try {
PreparedStatement search;
search = con.prepareStatement("SELECT * FROM tbl_name;");
rs = search.executeQuery();
}
catch (SQLException sqle) {
error = "SQLException: Could not execute the query";
throw new SQLException(error);
}
catch (Exception e) {
error = "An exception occured while retrieving records.";
throw new Exception(error);
}
return rs;
} // } end of class file
JSP Code: Inserting records from the MySQL database
The Java code below shows how to insert records from the database in JSP. This code snippet should be included in the Java class used to connect or disconnect to the database.
// class file {
// connect(){}
//disconnect(){}
public void uploadReqDocs(String examineeNum, String reqDocs,
String reqDocsDesc) throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement uploadReqDocs;
uploadReqDocs = con.prepareStatement("insert into"
+" tbl_transrequirements values(?, ?, ?);");
uploadReqDocs.setString(1, examineeNum);
uploadReqDocs.setString(2, reqDocs);
uploadReqDocs.setString(3, reqDocsDesc);
uploadReqDocs.execute();
} catch (SQLException sqle) {
error = "SQLException: update failed, possible duplicate entry";
throw new SQLException(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
// } end of class files
JSP Code: Connecting to MySQL Database
The code below shows how to connect to the MySQL database in Java Server Pages. The code shows only the connect and disconnect methods used to connect to the database. You need to have the MySQL Connector to be able to connect to MySQL. You can have it here.
Download: mysql-connector-java-3.0.11-stable-bin.jar
import java.sql.*;
import java.util.*;
public class ConnectionDB {
String error;
Connection con;
public ConnectionDB() { }
public void connect() throws ClassNotFoundException,
SQLException, Exception {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///dbname“, “
usrnme“, “pass“);
}
catch (ClassNotFoundException cnfe) {
error = “ClassNotFoundException: Could not locate DB driver.”;
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe) {
error = “SQLException: Could not connect to database.”;
throw new SQLException(error);
}
catch (Exception e) {
error = “Exception: An unknown error occured while connecting ”
+”to database.”;
throw new Exception(error);
}
}
public void disconnect() throws SQLException {
try {
if (con != null) {
con.close();
}
}
catch(SQLException sqle) {
error = (”SQLException: Unable to close the database connection.”);
throw new SQLException(error);
}
}
}


