package zmsjdbc;
import java.sql.*;
import java.sql.ResultSet;
/**
* 建一个ACCESS数据库 db1 有字段1 id 字符型, 2 name 字符型
*/
public class access1
{
public static void main(String[] args)
{
access1 x=new access1();
String str="select * from test1";
if (x.execute1(str))
{
System.out.println("提取成功");
}
}
public boolean execute1(String sqlstr)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:/accdb/db1.mdb";
Connection conn = DriverManager.getConnection(url, "", "");
Statement stmtNew = conn.createStatement();
ResultSet rs=stmtNew.executeQuery(sqlstr);
if(rs.next())
{
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
return true;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return false;
}
catch(Exception e)
{
System.out.println("出错了");
return false;
}
}
}