日期:2014-05-20  浏览次数:20841 次

一个JDBC 问题,望各位高手不吝赐教!
我想在JAVA中显示多行SQL   查询记录,该如何实现?谁能给我介绍一些JDBC的资料,小弟现在这鞠躬了!
我是个JAVA初学者,很多不懂,遇到问题没有良师益友的指点与讨论,一时半刻无法解决,欲哭无泪,谁愿意与我经常交流?我的邮箱是:yongjie66@126.com(最好是用邮箱联系,我们公司不允许上QQ);QQ是274375180(加我时注明:JAVA)!愿与同道中人携手共进!共讨JAVA!!!


------解决方案--------------------
import java.sql.*;
public class DbTest
{ private Connection conn=null;
private ResultSet rs=null;
private Statement stm=null;
public DbTest(){}
public void openOdbcDb(String dsnn,String user,String password)
{try{
String url= "jdbc:odbc: "+dsnn;
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
conn=DriverManager.getConnection(url,user,password);

}catch(Exception e){System.err.println( "aq.executeQuery: " + e.getMessage());}
}

public void openJdbcDb(String dsnn)
{openJdbcDb(dsnn, "sa ", "sa ");
}
public void openJdbcDb(String dsnn,String user,String password)
{try{Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName= "+dsnn;
conn= DriverManager.getConnection(url,user,password);
}catch(Exception e){System.err.println( "aq.executeQuery: " + e.getMessage());}
}
public ResultSet executeQuery(String sql)
{rs=null;
try{
stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stm.executeQuery(sql);

}catch(Exception e){System.err.println( "aq.executeQuery: " + e.getMessage());}
return rs;
}

public void executeUpdate(String sql)
{ try{
stm=conn.createStatement();
stm.executeUpdate(sql);
}catch(Exception e){System.err.println( "aq.executeQuery: " + e.getMessage());}
}
public void closeDb()
{if(rs!=null)
{try{rs.close();
rs=null;
}catch(Exception e){}
}
if(stm!=null)
{try{stm.close();
stm=null;
}catch(Exception e){}
}
if(conn!=null)
{try{
conn.close();
conn=null;
}
catch(Exception e){}
}
}
public static void main(String [] args)throws Exception
{DbTest test=new DbTest();
test.testDb( "select * from liuyan_tb join fuzhuang_tb on liuyan_tb.ID=fuzhuang_tb.ID ");

}
public void testDb(String sql)throws Exception
{ ResultSet rst=null;
openJdbcDb( "clother ");
System.out.println(sql);
rst=executeQuery(sql);
for(int j=1; j <=rst.getMetaData().getColumnCount(); j++)
{System.out.print(rst.getMetaData().getColumnName(j)+ " ");}
System.out.println( " ");
while (rst.next())
{ for(int j=1; j <=rst.getMetaData().getColumnCount(); j++)
{
System.out.print( rst.getObject(j)+ " ");
}
System.out.println( " ");
}

closeDb();
}
}