我想连接ACCESS,把下面这段代码写到了ECLIPSE中。但是在appliacation下有红线表示错误,有朋友说是我url的路径错了。如果是这个路径D:\driver\Classenviroment\JDBC\db1.mdb,要怎么写到这个句子中?
String url="/Data/ReportDemo.mdb";
String strurl="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath(url); //application在这
String url="/Data/ReportDemo.mdb";
String strurl="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath(url); //application在这
解决方案 »
- java作业-通讯录,无数据库,写入数据时出现乱码
- 在 while true 中如何实现 q 退出循环?
- 这个this何解?
- 如何生成jar文件?谢谢
- 如何校验不定长的字符串为数字型
- 请教问题 有分加
- *********极其有意思的问题(如何顺序获得JAVA文件中的方法名)************
- 使用jspsmartupload组件,上传文件到数据库时出错 ????????
- 主函数中的for循环时,每一个node都会分配一个nextNode吗?可是nextNode是成员变量,只有一个没有new啊!求解
- java中的io流问题
- 菜鸟:gui,想说爱你不容易?
- 如何给TextField 添加滚动条
http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51a-linux-i686.tar.gz/from/pick#mirrors
import java.sql.*;public class UNSDBConnection
{
//jdbc driver, jdbc-odbc bridge
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//the database to be connected
String dbName = "jdbc:odbc:user";
//database's user name
String dbUser = "";
//database's password
String dbPass= "";
/**
* constructor
* */
public UNSDBConnection()
{
try
{
Class.forName( dbDriver );
}
catch( ClassNotFoundException classNotFoundException )
{
System.err.println( "DBConnection(): " + classNotFoundException.getMessage() );
}
}
}
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“) ;
String url=“jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=“+application.getRealPath(“/Data/ReportDemo.mdb“);
Connection conn = DriverManager.getConnection(url,““,“”);
Statement stmtNew=conn.createStatement() ; <%
String sitePath = request.getRealPath("/");
String dbPath = sitePath+"db1.mdb";
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbPath;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
out.println("access yes1");
Connection conn2=DriverManager.getConnection(strurl);
out.println("access yes2");
Statement stmt=conn2.createStatement();
out.println("access yes3");
}
catch (Exception e)
{
out.println(e.getMessage());
}
%>
如果是这个路径D:\driver\Classenviroment\JDBC\db1.mdb,DBQ后面要怎么写?
String sitePath = request.getRealPath("D:/driver/Classenviroment/JDBC/");
String dbPath = sitePath+"db1.mdb";
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbPath;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
out.println("access yes1");
Connection conn2=DriverManager.getConnection(strurl);
out.println("access yes2");
Statement stmt=conn2.createStatement();
out.println("access yes3");
}
catch (Exception e)
{
out.println(e.getMessage());
}
try{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“) ;
String url=“jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=“+application.getRealPath(“D:\driver\Classenviroment\JDBC\db1.mdb“);
System.out.println("before connection");
Connection conn = DriverManager.getConnection(url,““,“”);
Statement stmtNew=conn.createStatement() ;
ResultSet rs=stmt.executeQuery("select * from test");
if(rs.next())
{
System.out.println(rs.getString("id"));
}
}catch(Exception e)
{
System.out.println(e);
}
如果你直接写在一个类里面 application 下面当然是红色的啦。
如果直接写成一个类 需要继承 HttpServlet
application 在servlet中的获得方
ServletContext application = request.getSession().getServletContext();
希望对你有所帮助