JSP访问数据库学习笔记

第一步:加载数据库驱动程序

mySql-------Class.forName("com.mysql.jdbc.Driver");

Oracle------Class.forName("oracle.jdbc.driver.OracleDriver);

......

第二步:链接数据库

Connection con=DriverManager.getConnection(url,user,pwd);

第三步:创建一个JDBC声明

Statement stmt=con.createStatement();

第四步:访问数据库

 

实例:

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=GB2312"
    pageEncoding="GB2312"%>




Insert title here


<%
 request.setCharacterEncoding("GB2312");
 Class.forName("oracle.jdbc.driver.OracleDriver");
 String url = "jdbc:oracle:thin:@localhost:1521:orcl";
 String user = "dbsnmp";
 String password = "dbsnmp";
 Connection conn = DriverManager.getConnection(url,user,password);
 Statement stmt = conn.createStatement();
 String sql = "select * from mytest2";
 ResultSet rs = stmt.executeQuery(sql);
 
 while(rs.next()){
  %>
  一:<%=rs.getString(1) %>
  二:<%=rs.getString(2) %>

  <%
  
 }
 out.print("success!");
 rs.close();
 stmt.close();
 conn.close();
 
%>

你可能感兴趣的:(学习笔记)