Connection con = null;
PreparedStatement stmt = null;
try
{
con = ...
stmt = con.prepareStatement(
"INSERT INTO Personen(Name, Alias, Größe, Rasse, Lieblingswaffe, Sternzeichen, "
+"Geburtsdatum, Beschreibung) VALUES(?, ?, ?, ?, ?, ?, ?, ?) "
);
DBUtil.setValue(1, stmt, neue_person.name);
DBUtil.setValue(2, stmt, neue_person.alias);
DBUtil.setValue(3, stmt, neue_person.size);
DBUtil.setValue(4, stmt, neue_person.race);
DBUtil.setValue(5, stmt, neue_person.weapon);
DBUtil.setValue(6, stmt, neue_person.star_sign);
DBUtil.setValue(7, stmt, neue_person.date);
DBUtil.setValue(8, stmt, neue_person.descripton);
if(stmt.executeUpdate() != 1)
{
Fehler
}
}
catch(SQLException e)
{
// Fehlerbehandlung
}
finally
{
if(stmt != null)
{
try { stmt.close(); } catch(SQLException e) { /*Fehlerbehandlung*/ }
}
if(con != null)
{
try { con.close(); } catch(SQLException e) { /*Fehlerbehandlung*/ }
}
}
-------------------------------
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public final class DBUtil
{
private DBUtil() {}
public static String getString(ResultSet rs, String fieldName) throws SQLException
{
String stringValue = rs.getString(fieldName);
return !rs.wasNull()? stringValue : null;
}
public static Boolean getBoolean(ResultSet rs, String fieldName) throws SQLException
{
boolean booleanValue = rs.getBoolean(fieldName);
return !rs.wasNull()? Boolean.valueOf(booleanValue) : null;
}
public static Integer getInteger(ResultSet rs, String fieldName) throws SQLException
{
int intValue = rs.getInt(fieldName);
return !rs.wasNull()? Integer.valueOf(intValue) : null;
}
public static Double getDouble(ResultSet rs, String fieldName) throws SQLException
{
double doubleValue = rs.getDouble(fieldName);
return !rs.wasNull()? Double.valueOf(doubleValue) : null;
}
public static Float getFloat(ResultSet rs, String fieldName) throws SQLException
{
float floatValue = rs.getFloat(fieldName);
return !rs.wasNull()? Float.valueOf(floatValue) : null;
}
public static java.util.Date getDate(ResultSet rs, String fieldName) throws SQLException
{
java.sql.Date dateValue = rs.getDate(fieldName);
return !rs.wasNull()? new java.util.Date(dateValue.getTime()) : null;
}
public static void setValue(int index, PreparedStatement stmt, String value) throws SQLException
{
if( value != null )
stmt.setString(index, value);
else
stmt.setNull(index, Types.VARCHAR);
}
public static void setValue(int index, PreparedStatement stmt, Boolean value) throws SQLException
{
if( value != null )
stmt.setBoolean(index, value.booleanValue());
else
stmt.setNull(index, Types.BOOLEAN);
}
public static void setValue(int index, PreparedStatement stmt, boolean value) throws SQLException
{
setValue(index, stmt, Boolean.valueOf(value) );
}
public static void setValue(int index, PreparedStatement stmt, Integer value) throws SQLException
{
if( value != null )
stmt.setInt(index, value.intValue());
else
stmt.setNull(index, Types.INTEGER);
}
public static void setValue(int index, PreparedStatement stmt, int value) throws SQLException
{
setValue(index, stmt, Integer.valueOf(value));
}
public static void setValue(int index, PreparedStatement stmt, Double value) throws SQLException
{
if( value != null )
stmt.setDouble(index, value.doubleValue());
else
stmt.setNull(index, Types.DOUBLE);
}
public static void setValue(int index, PreparedStatement stmt, double value) throws SQLException
{
setValue(index, stmt, Double.valueOf(value));
}
public static void setValue(int index, PreparedStatement stmt, Float value) throws SQLException
{
if( value != null )
stmt.setFloat(index, value.floatValue());
else
stmt.setNull(index, Types.FLOAT);
}
public static void setValue(int index, PreparedStatement stmt, float value) throws SQLException
{
setValue(index, stmt, Float.valueOf(value));
}
public static void setValue(int index, PreparedStatement stmt, java.util.Date value) throws SQLException
{
if( value != null )
stmt.setDate(index, new java.sql.Date(value.getTime()));
else
stmt.setNull(index, Types.DATE);
}
}