ps=conn.prepareStatement(sql.toString()); 调试结果:ps=null。拼接数来的sql语句在数据库中能查到数据
publicstaticvoidexport(StringtableName,StringtableColumn[],StringexcelColumn[],String...
public static void export(String tableName,String tableColumn[],String excelColumn[],String exportExcelPath)
{
StringBuffer sql=new StringBuffer("select ");
//String sql=new String("select ");
for(int i = 0 ;i<tableColumn.length;i++)
{
if(i==tableColumn.length-1)
{
sql.append(tableColumn[i]+" from "+tableName+";");
}else
{
sql.append(tableColumn[i]+",");
}
}
System.out.println("sql语句"+sql);
List<String[]> list=new ArrayList<String[]>();
//添加excel列名
list.add(excelColumn);
Connection conn=DBconn.getDBConnection();
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql.toString());
rs=ps.executeQuery();
while(rs.next())
{
String[] data=new String[list.get(0).length];
for(int i=0;i<tableColumn.length;i++)
{
data[i]=rs.getString(tableColumn[i]);
}
list.add(data);
System.out.println(list.size()+"-----");
}
System.out.println(sql.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DBconn.closeAll(rs, ps, conn);
}
sql查出9条数据(数据供9条),ps=null 展开
{
StringBuffer sql=new StringBuffer("select ");
//String sql=new String("select ");
for(int i = 0 ;i<tableColumn.length;i++)
{
if(i==tableColumn.length-1)
{
sql.append(tableColumn[i]+" from "+tableName+";");
}else
{
sql.append(tableColumn[i]+",");
}
}
System.out.println("sql语句"+sql);
List<String[]> list=new ArrayList<String[]>();
//添加excel列名
list.add(excelColumn);
Connection conn=DBconn.getDBConnection();
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql.toString());
rs=ps.executeQuery();
while(rs.next())
{
String[] data=new String[list.get(0).length];
for(int i=0;i<tableColumn.length;i++)
{
data[i]=rs.getString(tableColumn[i]);
}
list.add(data);
System.out.println(list.size()+"-----");
}
System.out.println(sql.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DBconn.closeAll(rs, ps, conn);
}
sql查出9条数据(数据供9条),ps=null 展开
1个回答
2012-10-28
展开全部
conn 这个对象没初始化成功 它 是null 你用 null.prepareStatement(strSql); 肯定会报空指针异常 建议: 检查 先检查连接是否正常 在进行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询