Java method for connecting to an access database

Source: Internet
Author: User
Tags driver manager
Recently, we need to use java for a small function. We need to link to the access database. I have found a lot on the Internet, and the methods are similar. I will summarize them for your reference. My office version is 2010, so the suffix of the access database file is. accdb. When publishing the system, the database file is put in the project and published with tomcat, which facilitates deployment.

Recently, we need to use java for a small function. We need to link to the access database. I have found a lot on the Internet, and the methods are similar. I will summarize them for your reference. My office version is 2010, so the suffix of the access database file is. accdb. When publishing the system, the database file is put in the project and published with tomcat, which facilitates deployment.

Recently, we need to use java for a small function.DatabaseI found a lot on the internet,MethodAll of them are similar. Here is a summary for your reference.

My office version is 2010, so accessDatabaseThe file suffix is. accdb. When the system is released,DatabaseThe file is released with tomcat in the project to facilitate deployment.

1. ObtainDatabasePath:

// ObtainDatabaseFile Path
Public static String getPath (){
String path = "";
String projectName = "Test ";
Path = System. getProperty ("user. dir"); // obtain the bin directory address of tomcat.
Path = path. replace ("bin", "webapps") + "\" + projectName + "\ Data \ test. accdb ";
Return path;

}

2. EstablishDatabaseConnection:

Url ,*. mdb ,*. there must be a space between accdb, otherwise an error will be reported: java. SQL. SQLException: [Microsoft] [ODBC driver manager] No data source name is found and no default driver is specified;

PWD isDatabasePassword. If not, you do not need to write it.

// CreateDatabaseLink
Public static Connection getConnection () throws SQLException, ClassNotFoundException {
     String path = getPath();
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+path+" ;PWD=123456789";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
return conn;

}

3. Simple data query:

// Return the first column of the first row of the query result based on the SQL statement.
Public static String getScalar (String strSQL ){
String rValue = "";
Connection conn = null;
Try {
Conn = getConnection ();
Statement st = conn. createStatement ();
// System. out. println (strSQL );
ResultSet rs = st.exe cuteQuery (strSQL );
If (rs. next ()){
RValue = rs. getString (1 );

}
Rs. close ();
St. close ();
Conn. close ();

} Catch (Exception e ){
System. out. println ("DatabaseConnection error. error message: "+ e. toString ());
} Finally {
Try {
If (conn! = Null &&! Conn. isClosed ())
Conn. close ();
} Catch (SQLException ex1 ){
System. out. println ("DatabaseDisable an error. error message: "+ ex1.toString ());
}
}
Return rValue;
}


I wrote it here first. For the first time I wrote a blog, I would like to give you some advice on some bad things or some inappropriate code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.