当前位置: 技术问答>java相关
我就不信了,难道没人会通过jdbc_odbc桥访问Excel表中数据???
来源: 互联网 发布时间:2015-07-22
本文导语: import java.sql.*; public class ExcelConn { public static void main(String args[]) { try{ String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; Class.forName(sDBDriver); Connection conn=DriverManager.getConnection("jdbc:odbc:exceltest"); Stat...
import java.sql.*;
public class ExcelConn
{
public static void main(String args[])
{
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(sDBDriver);
Connection conn=DriverManager.getConnection("jdbc:odbc:exceltest");
Statement stmt=conn.createStatement();
String sqlstr="";
ResultSet rs=stmt.executeQuery(sqlstr);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
各位帮忙看看sql语句该怎么写?Excel表名为aa
内容为:
aa bb cc
1 2 3
1 2 3
public class ExcelConn
{
public static void main(String args[])
{
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(sDBDriver);
Connection conn=DriverManager.getConnection("jdbc:odbc:exceltest");
Statement stmt=conn.createStatement();
String sqlstr="";
ResultSet rs=stmt.executeQuery(sqlstr);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
各位帮忙看看sql语句该怎么写?Excel表名为aa
内容为:
aa bb cc
1 2 3
1 2 3
|
import java.sql.*;
public class ExcelConn
{
public static void main(String args[])
{
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(sDBDriver);
Connection conn=DriverManager.getConnection("jdbc:odbc:exceltest");
Statement stmt=conn.createStatement();
String sqlstr="Select * From [Sheet1$]";
//或者写成String sqlstr="Select * From aa.[Sheet1$]";
ResultSet rs=stmt.executeQuery(sqlstr);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class ExcelConn
{
public static void main(String args[])
{
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(sDBDriver);
Connection conn=DriverManager.getConnection("jdbc:odbc:exceltest");
Statement stmt=conn.createStatement();
String sqlstr="Select * From [Sheet1$]";
//或者写成String sqlstr="Select * From aa.[Sheet1$]";
ResultSet rs=stmt.executeQuery(sqlstr);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|
难道不是这样写:select aa,bb,cc from aa,不过好像你的odbc的数据源没写好,好像是这样的,我记得:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"myLogin", "myPassword");
//String url = "jdbc:odbc:Fred";
//String myLogin= "username";
//String myPassword= "password";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"myLogin", "myPassword");
//String url = "jdbc:odbc:Fred";
//String myLogin= "username";
//String myPassword= "password";