Connect MS access database in JSP pages
I would like to ask you how can I connect a MS access database in JSP pages?Do you kn开发者_如何转开发ow any ready class which I can use?I am using Netbeans to create JSP pages!
Ancient question, but I shall answer anyway. Had to Google around a bit when the same question flummoxed me. Here goes (concise version of stuff found here):
Administratively Register Database
Use MS Access to create a blank database in some directory. (eg.
Database1.mdb
.) Make sure to close the data base after it is created.Go to: Control panel -> Admin tool -> ODBC
Under the System DSN tab (for Tomcat version 5 or later – User DSN for earlier versions), un-highlight any previously selected name and then click on the Add button.
On the window that then opens up, highlight MS Access Driver & click Finish.
On the ODBC Setup window that then opens, fill in the data source name. This is the name that you will use to refer to the data base in your Java program – like
arc
. This name does not have to match the file name.
.- Then click Select and navigate to the already created database in directory.
Suppose the file name isDatabase1.mdb
. After highlighting the named file,
click OKs all the way back to the original window.
Connect a JSP page to an Access Database
<%@ page import="java.sql.*" %>
<%
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=null;
conn = DriverManager.getConnection("jdbc:odbc:arc", "", "");
out.println ("Database Connected.");
%>
Now that the JDBC-ODBC Bridge has been removed from Java 8, the UCanAccess JDBC driver may be a more relevant option for future users. For details, see the related Stack Overflow question
Manipulating an Access database from Java without ODBC
精彩评论