链接数据库的类方法 package guides.jdbc.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResourceUtil { public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String ORACLE_URL = "jdbc:oracle:thin:@192.168.0.23:1521:tarena"; public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver"; public static final String MYSQL_URL = "jdbc:mysql://127.0.0.1:3306/jdbcguides"; public static Connection getConnection() { return getMySQLConnection(); } public static Connection getOracleConnection() { try { // register driver Class.forName(ORACLE_DRIVER); // get connection Connection con = DriverManager.getConnection(ORACLE_URL, "openlab", "open123"); return con; } catch (ClassNotFoundException e) { System.out.println("failed to register driver."); throw new RuntimeException(e); } catch (SQLException e) { System.out.println("failed to execute sql."); throw new RuntimeException(e); } } public static Connection getMySQLConnection() { try { // register driver Class.forName(MYSQL_DRIVER); // get connection Connection con = DriverManager.getConnection(MYSQL_URL, "root", "1234"); return con; } catch (ClassNotFoundException e) { System.out.println("failed to register driver."); throw new RuntimeException(e); } catch (SQLException e) { System.out.println("failed to execute sql."); throw new RuntimeException(e); } } public static void close(ResultSet rs, Statement st, Connection con) { close(rs); close(st, con); } public static void close(Statement st, Connection con) { try { st.close(); } catch (Exception e) { } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { rs.close(); } catch (Exception e) { } } } 链接MySQL package guides.jdbc.core; import java.sql.*; public class FirstMySQLJdbc { public static void main(String[] args) { Connection con = null; Statement st = null; try { // register driver Class.forName("com.mysql.jdbc.Driver"); // get connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbcguides", "root", "1234"); // create statement st = con.createStatement(); // execute sql String sql = "insert into guides_student(no, name, email) values('st001', 'tom', 'tom@163.com')"; int rows = st.executeUpdate(sql); // handle result System.out.println(rows + " rows affected."); } catch (ClassNotFoundException e) { System.out.println("failed to register driver."); } catch (SQLException e) { System.out.println("failed to execute sql."); } finally { // release resources try { st.close(); } catch (Exception e) { } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } } servlet配置信息web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>one</servlet-name> <servlet-class> org.whatisjava.servlet.SessionServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>one</servlet-name> <url-pattern>/session</url-pattern> </servlet-mapping> </web-app> servlet RequestDispatcher rd = request.getRequestDispatcher("/index.jsp"); rd.forward(request, response);