在最近的一个小系统里要加入一个功能,一个列表框,数据从数据库的字典表中取得,研究了一下,实现了,记录下
只显示一个字段的内容,我采用Vector来实现,代码如下
package
com
.
xx
.
work
.
data
;
import java . sql . ResultSet ;
import java . sql . SQLException ;
import java . util . Vector ;
import com . aheadsoft . work . dao . DBConnect ;
public class Add {
private int ID ;
private String workType ;
public int getID () {
return ID ;
}
public void setID ( int id ) {
ID = id ;
}
public String getWorkType () {
return workType ;
}
public void setWorkType ( String workType ) {
this . workType = workType ;
}
public Add (){
ID = 0 ;
workType = "" ;
}
public Add ( int ID , String workType ){
this . ID = ID ;
this . workType = workType ;
}
public void setID ( String newID ) {
if ( newID != null )
this . ID = Integer . parseInt ( newID ) ;
;
}
public static Vector excute () throws Exception {
Vector list = new Vector () ;
String Str = " select * from workType " ;
DBConnect dbc = null ;
ResultSet rs = null ;
try {
dbc = new DBConnect () ;
try {
rs = dbc . executeQuery ( Str ) ;
while ( rs . next ()) {
Add add = new Add () ;
add . setWorkType ( rs . getString ( " workType " )) ;
list . add ( add ) ;
}
} catch ( SQLException e ){
e . printStackTrace () ;
} finally {
if ( rs != null )
rs . close () ;
}
} catch ( SQLException e ) {
e . printStackTrace () ;
} finally {
if ( dbc != null )
dbc . close () ;
}
return list ;
}
public static void main ( String [] args ){
}
}
import java . sql . ResultSet ;
import java . sql . SQLException ;
import java . util . Vector ;
import com . aheadsoft . work . dao . DBConnect ;
public class Add {
private int ID ;
private String workType ;
public int getID () {
return ID ;
}
public void setID ( int id ) {
ID = id ;
}
public String getWorkType () {
return workType ;
}
public void setWorkType ( String workType ) {
this . workType = workType ;
}
public Add (){
ID = 0 ;
workType = "" ;
}
public Add ( int ID , String workType ){
this . ID = ID ;
this . workType = workType ;
}
public void setID ( String newID ) {
if ( newID != null )
this . ID = Integer . parseInt ( newID ) ;
;
}
public static Vector excute () throws Exception {
Vector list = new Vector () ;
String Str = " select * from workType " ;
DBConnect dbc = null ;
ResultSet rs = null ;
try {
dbc = new DBConnect () ;
try {
rs = dbc . executeQuery ( Str ) ;
while ( rs . next ()) {
Add add = new Add () ;
add . setWorkType ( rs . getString ( " workType " )) ;
list . add ( add ) ;
}
} catch ( SQLException e ){
e . printStackTrace () ;
} finally {
if ( rs != null )
rs . close () ;
}
} catch ( SQLException e ) {
e . printStackTrace () ;
} finally {
if ( dbc != null )
dbc . close () ;
}
return list ;
}
public static void main ( String [] args ){
}
}