JAVA连接Microsoft ACCESS数据库程序实例- -

JAVA连接Microsoft ACCESS数据库程序实例

import java.sql.*;

public class datebase {
Connection con;
ResultSet rs;
Statement stmt;

public datebase(){
//定义连接类型及连接数据库 ACCESS
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=LibraryBase.mdb";
con=DriverManager.getConnection(strurl) ;
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

}

//异常抛出
catch(Exception e)
{System.out.println(e); }
}

//定义数据库执行返回值 RS;
public ResultSet getResult(String strSQL)
{
try{
rs=stmt.executeQuery(strSQL);
return rs;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return null;
}

}

//定义数据库修改保存类
public boolean updateSql(String strSQL)
{
try{
stmt.executeUpdate(strSQL);
con.commit();
return true;

}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return false;
}

}

//定义关闭数据库类
public void closeConnection()
{
try
{
con.close();
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
}
}

你可能感兴趣的:(Microsoft)