package tools; import java.sql.*; import java.util.*; import javax.servlet.*; public class DB { ResultSet rset = null; Connection conn = null; public void connect(String db, String user, String passwd) throws Exception { String url = "jdbc:mysql://localhost.localdomain/" + db; DriverManager.registerDriver(new com.mysql.jdbc.Driver()); conn = DriverManager.getConnection(url, user, passwd); } public void connect(String user) throws Exception { connect(user, user, user); } public ResultSet query(String sql) throws Exception { return rset = conn.createStatement().executeQuery(sql); } public boolean next() throws Exception { return rset.next(); } public String get(String s) throws Exception { return rset.getString(s); } public void insert(String table, ServletRequest request) throws Exception { Enumeration parm = request.getParameterNames(); String fields = "", values = ""; boolean first = true; while (parm.hasMoreElements()) { String name = (String) parm.nextElement(); if (!first) { fields += ","; values += ","; } fields += name; values += "'" + request.getParameter(name) + "'"; first = false; } update("insert into " + table + " (" + fields + ") values (" + values + ")"); } public void update(String sql) throws Exception { conn.createStatement().executeUpdate(sql); } }