📄 dbutil.java
字号:
}
return false;
}
@SuppressWarnings("unchecked")
public static int update(Table table)
{
if(table.getKeys()!=null&&table.getKeys().size()>0)
{
JdbcContextFactory factory=JdbcContextFactory.newInstance();
Context context=factory.newJdbcContext();
SqlGenerator generator=new SqlGeneratorImplement();
String sql=generator.updateSql(table);
System.out.println(sql);
List properties=(ArrayList)getUpdateProperties(table);
Object[] param=properties.toArray();
try
{
return context.updateRecord(sql, param);
}
catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
else
{
JdbcContextFactory factory=JdbcContextFactory.newInstance();
Context context=factory.newJdbcContext();
SqlGeneratorImplement generator=new SqlGeneratorImplement();
String sql=generator.updateSql(table, false);
List properties=(ArrayList)getUpdateProperties(table,false);
Object[] param=properties.toArray();
try
{
return context.updateRecord(sql, param);
}
catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
}
@SuppressWarnings("unchecked")
public static List getAll(Table table)
{
DBProcess processor=new DBWrapper();
List records=new ArrayList();
try
{
Connection connection=processor.getConnection();
List keys=table.getKeys();
StringBuffer sql=new StringBuffer("select ");
for(int i=0;i<keys.size();i++)
{
Map key=(Map)keys.get(i);
Set set=key.entrySet();
Iterator it=set.iterator();
if(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
sql.append((String)entry.getKey()+",");
}
}
sql.setCharAt(sql.length()-1, ' ');
String querySql=sql.toString();
querySql+=" from "+table.getSimpleName();
PreparedStatement sm=connection.prepareStatement(querySql);
ResultSet rs=sm.executeQuery();
while(rs.next())
{
Serializable ids[]=new Serializable[keys.size()];
for(int i=0;i<keys.size();i++)
{
Map key=(Map)keys.get(i);
Set set=key.entrySet();
Iterator it=set.iterator();
if(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String keyName=(String)entry.getKey();
ids[i]=(Serializable) rs.getObject(keyName);
}
}
Object o=get(table,ids);
records.add(o);
}
}
catch (SQLException e)
{
log.debug("SQLExcpetion:", e);
}
return records;
}
@SuppressWarnings("unchecked")
private static List getProperties(Table table)
{
Class tableClass=table.getClass();
List result=new ArrayList();
Field[] fields=tableClass.getDeclaredFields();
for(int i=0;i<fields.length;i++)
{
Field field=fields[i];
field.setAccessible(true);
try
{
result.add(field.get(table));
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
return result;
}
@SuppressWarnings("unchecked")
public List getKeyNames(Table table)
{
List keys=table.getKeys();
List keyName=new ArrayList();
for(int i=0;i<keys.size();i++)
{
Map map=(HashMap)keys.get(i);
Set set=map.entrySet();
Iterator it=set.iterator();
if(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
keyName.add((String)entry.getKey());
}
}
return keyName;
}
@SuppressWarnings("unchecked")
private static List getKeys(Table table)
{
Class tableClass=table.getClass();
List result=new ArrayList();
try
{
List list=table.getKeys();
for(int i=0;i<list.size();i++)
{
Map column=(HashMap)list.get(i);
Set set=column.entrySet();
Iterator it=set.iterator();
while(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String keyValue=(String)entry.getKey();
Field field=tableClass.getDeclaredField(keyValue.toLowerCase());
field.setAccessible(true);
result.add(field.get(table));
}
}
return result;
}
catch (Exception e)
{
log.debug("The Exception is:", e);
}
return null;
}
@SuppressWarnings("unchecked")
public List query(String tableName,String queryColumn,String conditionColumn,Serializable value)
{
DBProcess processor=new DBWrapper();
SqlGenerator generator=new SqlGeneratorImplement();
String sql=((SqlGeneratorImplement) generator).query(tableName, queryColumn, conditionColumn);
System.out.println(sql);
List records=new ArrayList();
Serializable[] values={value};
try
{
Class c=Class.forName("hunnu.edu.cn.product.common.db.model."+tableName);
List list=processor.queryRecord(sql,values);
for(int i=0;i<list.size();i++)
{
Map map=(HashMap)list.get(i);
Set set=map.entrySet();
Iterator it=set.iterator();
Table record=(Table)c.newInstance();
while(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String key=(String)entry.getKey();
Object o=entry.getValue();
Field field=c.getDeclaredField(key);
field.setAccessible(true);
field.set(record, o);
}
records.add(record);
}
}
catch (Exception e)
{
log.debug("Exception:", e);
}
return records;
}
@SuppressWarnings("unchecked")
public List query(String tableName,String[] queryColumns,String[] conditionColumns,Serializable[] values)
{
DBProcess processor=new DBWrapper();
SqlGenerator generator=new SqlGeneratorImplement();
String sql=((SqlGeneratorImplement) generator).query(tableName, queryColumns, conditionColumns);
System.out.println(sql);
List records=new ArrayList();
try
{
Class c=Class.forName("hunnu.edu.cn.product.common.db.model."+tableName);
List list=processor.queryRecord(sql,values);
for(int i=0;i<list.size();i++)
{
Map map=(HashMap)list.get(i);
Set set=map.entrySet();
Iterator it=set.iterator();
Table record=(Table)c.newInstance();
while(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String key=(String)entry.getKey();
Object o=entry.getValue();
Field field=c.getDeclaredField(key);
field.setAccessible(true);
field.set(record, o);
}
records.add(record);
}
}
catch (Exception e)
{
log.debug("Exception:", e);
}
return records;
}
@SuppressWarnings("unchecked")
public List query(Table table,String[] queryColumns,String[] conditionColumns,Serializable[] values)
{
DBProcess processor=new DBWrapper();
SqlGenerator generator=new SqlGeneratorImplement();
String sql=((SqlGeneratorImplement) generator).query(table, queryColumns, conditionColumns);
System.out.println(sql);
String tableName=table.getSimpleName();
List records=new ArrayList();
try
{
Class c=Class.forName("hunnu.edu.cn.product.common.db.model."+tableName);
List list=processor.queryRecord(sql,values);
for(int i=0;i<list.size();i++)
{
Map map=(HashMap)list.get(i);
Set set=map.entrySet();
Iterator it=set.iterator();
Table record=(Table)c.newInstance();
while(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String key=(String)entry.getKey();
Object o=entry.getValue();
Field field=c.getDeclaredField(key);
field.setAccessible(true);
field.set(record, o);
}
records.add(record);
}
}
catch (Exception e)
{
log.debug("Exception:", e);
}
return records;
}
@SuppressWarnings("unchecked")
public List query(Table table,String queryColumn,String conditionColumn,Serializable[] values)
{
DBProcess processor=new DBWrapper();
SqlGenerator generator=new SqlGeneratorImplement();
String sql=((SqlGeneratorImplement) generator).query(table, queryColumn, conditionColumn);
System.out.println(sql);
String tableName=table.getSimpleName();
List records=new ArrayList();
try
{
Class c=Class.forName("hunnu.edu.cn.product.common.db.model."+tableName);
List list=processor.queryRecord(sql,values);
for(int i=0;i<list.size();i++)
{
Map map=(HashMap)list.get(i);
Set set=map.entrySet();
Iterator it=set.iterator();
Table record=(Table)c.newInstance();
while(it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next();
String key=(String)entry.getKey();
Object o=entry.getValue();
Field field=c.getDeclaredField(key);
field.setAccessible(true);
field.set(record, o);
}
records.add(record);
}
}
catch (Exception e)
{
log.debug("Exception:", e);
}
return records;
}
@SuppressWarnings("unchecked")
public static List getUpdateProperties(Table table,boolean hasKey)
{
if(hasKey)
return getUpdateProperties(table);
else
{
List properties=new ArrayList();
Class c=table.getClass();
Field fields[]=c.getDeclaredFields();
for(int i=0;i<fields.length;i++)
{
fields[i].setAccessible(true);
try
{
Object o=fields[i].get(table);
if(o!=null)
properties.add(o);
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
return properties;
}
}
public static void downLoadBAll(int pro_id,HttpServletResponse response)
{
FileInputStream inFile=null;
DBProcess process=new DBWrapper();
Connection connection=null;
String filePath="allFile.zip";
try
{
connection=process.getConnection();
connection.setAutoCommit(false);
Statement statement=connection.createStatement();
String sql="select file_stg_blob ,file_name from tb_pro_file left outer join tb_storage using(file_stg_id) where pro_id="+pro_id;
FileOutputStream file = new FileOutputStream(filePath);
ZipOutputStream zipFile = new ZipOutputStream(file);
ResultSet resultSet=statement.executeQuery(sql);
while(resultSet.next())
{
ZipEntry zipEntry = new ZipEntry(resultSet.getString(2)+".zip");
zipFile.putNextEntry(zipEntry);
BLOB blob=(BLOB)resultSet.getBlob(1);
InputStream in=blob.getBinaryStream();
byte[] buff = new byte[3000];
int len;
while((len = in.read(buff))>0)
{
zipFile.write(buff,0,len);
}
}
zipFile.flush();
connection.commit();
inFile = new FileInputStream(filePath);
response.setContentType("application/unknown");
response.addHeader("Content-Disposition", "attachment; filename="+filePath);
OutputStream out =response.getOutputStream();
IO.copyIO(inFile, out);
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(connection!=null)
process.closeConnection(connection);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -