//ODBC连接Excel
public static void main(String[] args) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
//加载ODBC驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//创建连接对象
conn = DriverManager.getConnection("jdbc:odbc:myexcel");
stm = conn.createStatement();//创建Statement对象
String sql = "select * from [sheet1$]";//查询excel中的工作表时后面要加上$符号并在最外层加上[]
rs = stm.executeQuery(sql);//执行sql语句,得到查询结果集
//遍历结果集
while(rs.next()){
//输出每一行记录的相关数据
//System.out.println(rs.getString("name")+"-"+rs.getInt("age"));
System.out.println(rs.getString(1)+"-"+rs.getInt(2));//可以通过上一行代码利用列名进行获取数据,也可以用列数来获取数据,列数从1开始
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
原文地址:http://www.cnblogs.com/danmao/p/3825272.html