有关Jsp连接Access以及JDBC,SQL的问题
发布网友
发布时间:2022-10-10 08:59
我来回答
共2个回答
热心网友
时间:2023-10-06 03:42
jsp连接access数据库不需要装jdbc
给你个servlet的例子吧,可以直接运行的.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class ConnectAccess extends HttpServlet {
Connection conn = null;
Statement stmt = null;
public void init() throws ServletException {
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\books.mdb";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(strurl);
stmt=conn.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;");
PrintWriter out = response.getWriter();
try {
ResultSet rs=stmt.executeQuery("select * from books");
out.println("<html><body>");
out.println("<p>Database connection successfully created!</p>");
while (rs.next()) {
out.println("<p>The book's name is: " + rs.getString("book") +"</p>");
}
out.println("</body></html>");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void destroy() {
try {
conn.close();
} catch(SQLException e){}
}
}
热心网友
时间:2023-10-06 03:43
jsp 连接数据库 只要配置好数据源 然后使用 代码连接就可以了啊!