哎 我给你最简单的例子
两个简单的jsp页面,数据库连接(我给你的是mysql数据库连接示例,后面附sqlserver数据库连接部分关键代码)
首先是 获取值页面My.jsp 源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'My.jsp' starting page 处理页面 Hp.jsp 源码:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'Hp.jsp' starting page <%
Connection con = null;
Statement stm = null;
String url = "jdbc:mysql://localhost:3306/数据名称";//数据库名称就是你的数据库名字
String driver = "com.mysql.jdbc.Driver"; //驱动类位置
String username = "root"; //数据库登录名称,此处写上你的用户名称
String pwd = "root"; //数据库登录密码,此处写上你的登录密码
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, username, pwd); //创建Connection连接对象
stm = con.createStatement(); //创建Statement 命令执行对象
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name=request.getParameter("name"); //获取传过来的名称
String password=request.getParameter("password");//获取传过来的密码
String sql="insert into user(name,password) values("+name+","+password+")";//数据库添加一条记录sql语句
int temp=stm.executeUpdate(sql);
if(temp>0)
{
out.print("添加成功");
}
else
{
out.print("添加失败");
}
//关闭数据库连接
stm.close();
con.close();