程序报这样的错 Exception in thread "main" java.sql.SQLException: Before start of result set
publicclassMyDataSource{privatestaticResultSetrs;publicstaticDataSourcegetsource(){tr...
public class MyDataSource {
private static ResultSet rs;
public static DataSource getsource(){
try {
Properties p = new Properties();
// InputStream in = MyDataSource.class.getClassLoader().getResourceAsStream("src/mydbutils/config.properties");
InputStream in = new FileInputStream("src/mydbutils/config.properties");
if(in!=null)
p.load(in);
DataSource ds= BasicDataSourceFactory.createDataSource(p);
return ds;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws SQLException {
DataSource ds = MyDataSource.getsource();
Connection con= ds.getConnection();
Statement st= con.createStatement();
rs= st.executeQuery("select * from student");
ResultSetMetaData rsmd= rs.getMetaData();
int a =rsmd.getColumnCount();
String name;
System.out.println(a);
for(int i=0;i<a;i++){
name= rsmd.getColumnName(i+1);
System.out.println(name);
sysotem.out.print( rs.getObject(name)); //这一句出的错别的没问题
}
}
} 展开
private static ResultSet rs;
public static DataSource getsource(){
try {
Properties p = new Properties();
// InputStream in = MyDataSource.class.getClassLoader().getResourceAsStream("src/mydbutils/config.properties");
InputStream in = new FileInputStream("src/mydbutils/config.properties");
if(in!=null)
p.load(in);
DataSource ds= BasicDataSourceFactory.createDataSource(p);
return ds;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws SQLException {
DataSource ds = MyDataSource.getsource();
Connection con= ds.getConnection();
Statement st= con.createStatement();
rs= st.executeQuery("select * from student");
ResultSetMetaData rsmd= rs.getMetaData();
int a =rsmd.getColumnCount();
String name;
System.out.println(a);
for(int i=0;i<a;i++){
name= rsmd.getColumnName(i+1);
System.out.println(name);
sysotem.out.print( rs.getObject(name)); //这一句出的错别的没问题
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询