Upload
meghan-holmes
View
212
Download
0
Embed Size (px)
Citation preview
Fall Extension ProjectTasks Needed for MissionMapEditor
Martin Q. ZhaoSeptember 18, 2010
DB Connectivity In MMEditorP
rovi
de
hig
h
leve
l fu
nct
ion
s
Pro
vid
e
qu
ery
le
vel
fun
ctio
ns
En
cap
sul
ate
JD
BC
co
nst
ruct
s: D
rive
r,
Con
nect
ion
, S
tate
men
t
DAO
JDBC Framework
DBTable
Table
DBMS
Application(client)
DB Server
Driver
Connection
Statement
ResultSet
creates
executes
returns
java.sqlMMEditor
MySQLServer Instance
mysql-connector.jar
implements
New DB Functions 4 MMEditorAdd
updateMapToDb
Add updateMissio
ndeleteMission
Similar design can be used in VulnerabilityTracker
Sample JDBC CodeGet connection to DB server
Run query and process results
// set driverString driverName =
"com.mysql.jdbc.Driver";try { Class.forName(driverName);} catch (ClassNotFoundException e) {}
// get connectionurlBase =
"jdbc:mysql://localhost:3306/“try { conn = DriverManager
.getConnection(urlBase + dbName, user, pwd);
} catch (SQLException e) {}
// get statement, which embodies queriestry { stmt = conn.createStatement();} catch (SQLException e) {}
Driver
Connection
Statement
ResultSet
creates
executes
returns
java.sql
Sample JDBC Code - continuedExecute update commands Run query and process
results// execute an insert statementString insertCommand = "INSERT INTO
Mission VALUES (";insertCmd += m.getId() + ", '" +
m.getName() +"', null, 1, " + lookupMissionId(m.getType()) +
",1";insertCommand += ")";
int rowsAffected = 0;try { rowsAffected =
stmt.executeUpdate(insertCmd);} catch (SQLException e) {}
// execute an select queryString query = "SELECT SubMissionId
FROM MissionSubMissionMap "+
"WHERE MissionId = " + parentMissionId;
LinkedList<Long> ids = new LinkedList<Long>();
try { ResultSet rs =
stmt.executeQuery(query); while (rs.next()) { ids.addLast(rs.getLong(1)); }} catch (SQLException e) {}