先上代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class My_Servlet extends HttpServlet {
private static Connection conn = null;
private static String driver = "com.mysql.jdbc.Driver";
//oracle的代码 private static String OracleDriver = "oracle.jdbc.driver.OracleDriver";
pricate static String dbName = "test";
private static String username = "root";
private static String password = "123456";
private static String sql = "select * from book";
private static String url = "jdbc:mysql://localhost:3306/"+dbName;
//oracle的代码 private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
public Servlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
try {
if (conn != null && (!conn.isClosed())) {
conn.close();
conn = null;
}
} catch (Exception e) {
System.out.println("Destroy Error:" + e.getMessage());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
// request.setCharacterEncoding("utf-8");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
try {
PreparedStatement ps = conn.prepareStatement(sql);
out.println("<h1>book表</h1>");
out.print("<table border='1'>");
out.print("<tr>");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
out.print("<tr>");
out.print("<td>" + " Id : " + rs.getInt(1) + "</td>");
out.print("<td>" + " Name : " + rs.getString(2) + "</td>");
out.print("<td>" + " Publisher : " + rs.getString(3)
+ "</td>");
out.print("<td>" + " PubDate: " + rs.getDate(4) + "</td>");
out.print("<td>" + " Price : " + rs.getBigDecimal(5)
+ "</td>");
out.println("</tr>");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
try {
Class.forName(driver);
//oracle的代码 Class.forName(OracleDriver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn == null) {
System.out.println("与mysql数据库连接失败!");
} else {
System.out.println("与mysql数据库连接成功!");
}
}
}
这是servlet的代码。然后需要在数据库中创建数据库。
其实mysql与oracle的区别仅仅在与驱动和url设置方面有些不同。
具体驱动mysql的需要下载 oracle10g的在如下安装目录中 oracle\product\10.2.0\db_1\jdbc\lib 中的 ojdbc14.jar jar包。