search方法:
public void search(){
SearchDialog sd=new SearchDialog(f,"查询",true);
sd.show();
if(sd.getActionCode()==sd.OK){
String readSql=sd.getSQL();
SqlUtil.readDBToTable(con,readSql,model,dataType);
}
}getSQL方法:
public String getSQL(){
String sql="select * from bookInfo where ";
if(radID.isSelected())
sql=sql+" bookID like '*"+txtID.getText().trim()+"*'";
else if(radBookName.isSelected())
sql=sql+" bookName like '*"+txtBookName.getText().trim()+"*'";
else if(radBookPress.isSelected())
sql=sql+" bookPress like '*"+txtBookPress.getText().trim()+"*'";
return sql;
}
readDBToTable方法:
public static void readDBToTable(Connection con,String readSql,
CustomTableModel model, Class []dataType){
clearAllRowsInTable(model);
PreparedStatement query=null;
try{
query= con.prepareStatement(readSql);
query.clearParameters();
ResultSet rs=query.executeQuery();
while(rs.next()){
int column=model.getColumnCount();
Object []line=new Object[column];
for(int i=0;i<column;i++){
if(dataType[i]==java.lang.String.class)
line[i]=rs.getString(i+1).trim();
else if(dataType[i]==java.lang.Double.class){
if(rs.getDouble(i+1)==Double.NEGATIVE_INFINITY)
line[i]=null;
else
line[i]=new Double(rs.getDouble(i+1));
}
else if(dataType[i]==java.lang.Integer.class){
if(rs.getInt(i+1)==Integer.MIN_VALUE)
line[i]=null;
else
line[i]=new Integer(rs.getInt(i+1));
}
}
model.addRow(line);
}
}catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"从数据库读取数据时发生错误!"+
"SQL语句为:"+readSql+e,"提示",
JOptionPane.ERROR_MESSAGE);
}
finally{
if(query!=null)
try{
query.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
public void search(){
SearchDialog sd=new SearchDialog(f,"查询",true);
sd.show();
if(sd.getActionCode()==sd.OK){
String readSql=sd.getSQL();
SqlUtil.readDBToTable(con,readSql,model,dataType);
}
}getSQL方法:
public String getSQL(){
String sql="select * from bookInfo where ";
if(radID.isSelected())
sql=sql+" bookID like '*"+txtID.getText().trim()+"*'";
else if(radBookName.isSelected())
sql=sql+" bookName like '*"+txtBookName.getText().trim()+"*'";
else if(radBookPress.isSelected())
sql=sql+" bookPress like '*"+txtBookPress.getText().trim()+"*'";
return sql;
}
readDBToTable方法:
public static void readDBToTable(Connection con,String readSql,
CustomTableModel model, Class []dataType){
clearAllRowsInTable(model);
PreparedStatement query=null;
try{
query= con.prepareStatement(readSql);
query.clearParameters();
ResultSet rs=query.executeQuery();
while(rs.next()){
int column=model.getColumnCount();
Object []line=new Object[column];
for(int i=0;i<column;i++){
if(dataType[i]==java.lang.String.class)
line[i]=rs.getString(i+1).trim();
else if(dataType[i]==java.lang.Double.class){
if(rs.getDouble(i+1)==Double.NEGATIVE_INFINITY)
line[i]=null;
else
line[i]=new Double(rs.getDouble(i+1));
}
else if(dataType[i]==java.lang.Integer.class){
if(rs.getInt(i+1)==Integer.MIN_VALUE)
line[i]=null;
else
line[i]=new Integer(rs.getInt(i+1));
}
}
model.addRow(line);
}
}catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"从数据库读取数据时发生错误!"+
"SQL语句为:"+readSql+e,"提示",
JOptionPane.ERROR_MESSAGE);
}
finally{
if(query!=null)
try{
query.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
解决方案 »
- 怎么让javac的提示显示英文
- 请问如何用java获取麦克的音量
- 请教一个java中的同步问题
- 谁知道JFrame上如何添加图片
- 谁能给一个简单的用java实现telnet的代码,无限感激
- 如何实现类似于秒表的功能
- 我使用was5.0发布程序, 为什么在我的根目录下比如明明有1.gif 使用http://localhost:9080/1.gif却报404错误?? jsp的就可以访问到,其
- interrupt的疑问
- applet在本地看正常,放到服务器上却显示不了,请问这是为什么?
- 谁能告诉我在那儿可以找到java内存管理软件boundcheck(名字有可能有点误差)
- 从Object到String的强制转型
- jar文件搜索
public class CustomTableModel extends DefaultTableModel{
String [] columnNames;
Class []dataType;
public CustomTableModel(int r,int c,String []cn,Class []dataType){
super(r,c);
columnNames=cn;
this.dataType=dataType;
}
public boolean isCellEditable(int row, int col){
return false;
}
public String getColumnName(int c){
return columnNames[c];
}
public Class getColumnClass(int c){
return dataType[c];
}
}
就只写一个sql语句,如果查询成功,再写if else。
如果不成功,就一点点的打印sql语句看哪里有错。。
把拼装后的sql打印出来, 放到数据库查询器里试试,