/**
*Usage for this class:
* java -cp /path/to/mm.mysql.jar:. jdbentry DBNAME TABLENAME
*
* This can be used as a standalone "application", or as part of a * larger program. It creates a top-level Frame, and connects to * a mysql database.table. * (Although in principle, it should be trivially usable with other * databases, if you get the right JDBC driver, and adjust getConn()) *
* It looks up existing values for each of the columns, and allows the * user to input a new row, either from scratch, or as a mix of * new column data and selected old column data *
* Requirements: * The name of a local MySQL database and a table. * The gjt.org JDBC driver in a .jar file (added to your classpath) * Permission for the user running this to modify the database, without * password. (or you could change the code, of course) *
* *
* This class was originally created for use with a "personal expenses"
* table. But I'm sure there are many, many other uses :-)
*/
/* You are free to use this class anywhere and any way you want. But
* it would be nice if you gave me credit somewhere.
* phil@bolthole.com http://www.bolthole.com
* @(#) jdbentry.java 1.3@(#)
*/
import java.sql.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
public class jdbentry implements WindowListener,ActionListener {
Connection dbconn=null;
String dbname,tablename;
Vector colnames, fields,choices;
Label status;
int addtotal=0;
static void debug(String msg){
System.out.println(msg);
}
TextField getTFAt(int colnum){
return (TextField)fields.elementAt(colnum);
}
Choice getChoiceAt(int colnum){
return (Choice)choices.elementAt(colnum);
}
String getColName(int colnum){
return (String)colnames.elementAt(colnum);
}
/* Looks at "global" vars dbname and tablename. But hardcoded
* to connect to mysql running on localhost
*/
void getConn(){
debug(" Connecting to database "+dbname);
String constring;
// name of database to connect to
constring="jdbc:mysql://localhost/"+dbname;
try{
/* THis is the good stuff, currently*/
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
String username=null,password=null;
dbconn=DriverManager.getConnection (constring,username,
password);
} catch (Exception err){
System.out.println("Got error connecting: "+err.getMessage());
}
}
/* colnum index starts at 0*/
Panel makePanel(int colnum){
Panel tmpp = new Panel();
String colname=getColName(colnum);
Label collabel=new Label(colname);
tmpp.add(collabel);
TextField tinput=new TextField(20);
tmpp.add(tinput);
fields.addElement(tinput);
Choice tmpc=makeChoice(colname);
tmpp.add(tmpc);
choices.addElement(tmpc);
return tmpp;
}
/* get the first 30 unique names for a column. Make a Choice widget */
Choice makeChoice(String colname){
Choice tmpchoice=new Choice();
tmpchoice.add("Input New Value");
Statement query=null;
ResultSet results;
try {
query=dbconn.createStatement();
results=query.executeQuery("select distinct "+colname+
" from "+ tablename);
int colcount=0;
while(results.next()){
colcount++;
String name=results.getString(1);
tmpchoice.add(name);
if(colcount>30)
break;
}
query.close();
} catch (SQLException err){
debug("cannot look up values; "+err.getMessage());
quit();
}
return tmpchoice;
}
/* Make an input panel for each column in the database */
void makeSubPanels(Panel topp){
for(int pcount=0;pcount