ตัวอย่าง JDBC ( Java Database Connectivity ) ของ servlet

NOTE : JDBC จะต่อ database ออก socket ตลอด แม้ว่าจะลง database กับ java อยู่ในเครื่องเดียวกันก็ตาม

import java.sql.*;
...
try {
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection(
"jdbc:postgresql:tomcat", "username", "password" );
PreparedStatement stmt =
db.prepareStatement( "select * from students" );
ResultSet rs = stmt.executeQuery();
writer.println( "<h3>Web nerds:</h3>" );
while ( rs.next() ) {
writer.println( "<p>" + rs.getString( "name" ) +
" is a Web Nerd.</p>" );
}
rs.close();
db.close();
} catch ( java.lang.ClassNotFoundException e ) {
writer.println( "<h3>Cannot load PostgreSQL driver!</h3>" );
e.printStackTrace( writer );
} catch ( java.sql.SQLException e ) {
writer.println( "<h3>SQL Exception!</h3>" );
e.printStackTrace( writer );
}

อันนี้สามารถเอาไปใส่ใน src ของ tomcat ได้ด้วยนะ

แต่ว่าไม่แนะนำ

รายละเอียดเพิ่มเติม

JDBC : http://java.sun.com/javase/6/docs/technotes/guides/jdbc/index.html

javax.sql JavaDocs : http://java.sun.com/javase/6/docs/api/javax/sql/package-summary.html

ตัวอย่าง JDBC เบื้องต้น

select

class Person…

private Connection db;

private final static String findStatementString =
“SELECT id, lastname, firstname, number_of_dependents” +
” FROM people” +
” WHERE id = ?”;
public static Person find(Long id) {
Person result = (Person) Registry.getPerson(id);
if (result != null) return result;
PreparedStatement findStatement = null;
ResultSet rs = null;
try {
findStatement = db.prepare(findStatementString);
findStatement.setLong(1, id.longValue());
rs = findStatement.executeQuery();
rs.next();
result = load(rs);
return result;
} catch (SQLException e) {
throw new ApplicationException(e);
} finally {
DB.cleanUp(findStatement, rs);
}
}
public static Person load(ResultSet rs) throws SQLException {
Long id = new Long(rs.getLong(1));
Person result = (Person) Registry.getPerson(id);
if (result != null) return result;
String lastNameArg = rs.getString(2);
String firstNameArg = rs.getString(3);
int numDependentsArg = rs.getInt(4);
result = new Person(id, lastNameArg, firstNameArg, numDependentsArg);
Registry.addPerson(result);
return result;
}

Insert/Update

class Person…
private final static String updateStatementString =
“UPDATE people” +
” set lastname = ?, firstname = ?, number_of_dependents = ?” +
” where id = ?”;
public void update() {
PreparedStatement updateStatement = null;
try {
updateStatement = db.prepare(updateStatementString);
updateStatement.setString(1, lastName);
updateStatement.setString(2, firstName);
updateStatement.setInt(3, numberOfDependents);
updateStatement.setInt(4, getID().intValue());
updateStatement.execute();
} catch (Exception e) {
throw new ApplicationException(e);
} finally {
DB.cleanUp(updateStatement);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s