日期:2014-05-16 浏览次数:21036 次
static class MyConnention{ public static Connection getConnection() throws Exception{ /**配置数据源后*/ // String URL="jdbc:odbc:<MS Access Database配置数据源名称>"; // Connection c=DriverManager.getConnection(URL,"",""); /**不配置数据源*/ //jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password] String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection c=DriverManager.getConnection(url1); return c; } }
static class MyConnention{ public static Connection getConnection() throws Exception{ /**不配置数据源*/ //jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password] Class theClass=MyConnention.class; java.net.URL u= theClass.getResource("db/db1.accdb"); String url=u.getPath().substring(1,u.getPath().length()).replaceAll("%20", " "); String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+url+";pwd=qing"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection c=DriverManager.getConnection(url1); return c; } }
public class StoreSample extends HttpServlet { private static final long serialVersionUID = 1L; static Connection theConn; /** * @see HttpServlet#HttpServlet() */ public StoreSample() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { theConn=StoreSample.MyConnention.getConnection(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } ResultSet rs; Statement stmt; String sql="select * from asspay"; try { stmt=theConn.createStatement(); rs=stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString("type")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //response.sendRedirect("/storeRemote.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } static class MyConnention{ public static Connection getConnection() throws Exception{ /**配置数据源后*/ // String URL="jdbc:odbc:MS Access Database"; // Connection c=DriverManager.getConnection(URL,"",""); /**不配置数据源*/ //jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password] String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection c=DriverManager.getConnection(url1); return c; } } }
public class CRSampleDataSourceBean { private static ResultSet resultSet = null; private static Connection connection = null; private static String connectionURL = "jdbc:odbc:Xtreme Sample Database 11.5"; private static String databaseClass = "sun.jdbc.odbc.JdbcOdbcDriver"; private static String query = "select * from customer"; public