我大致一个小例子你看看。
zhuce.html
<html>
<body>
<form name="form1" method="post" action="register.jsp">
<p align="center">用户名:
<input type="text" name="name">
</p>
<p align="center">密码:
<input type="password" name="password">
</p>
<p align="center">
<input type="submit" name="Submit" value=" 注册">
</p>
</form>
</body>
</html>
register.jsp
<%@ page contentType="text/htmlcharset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<body>
<%
request.setCharacterEncoding("GBK")
String name=request.getParameter("name")//内置对象应该会吧
String password=request.getParameter("password")
try{
Class.forName("org.gjt.mm.mysql.Driver")//驱动程序你自己的,我的是com.mysql.jdbc.Driver
String url="jdbc:mysql://localhost:3306/tian"//你自己设置数据库名称
Connection con=DriverManager.getConnection(url,"root","")//如果你mysql中root的密码是空的话最好写成""代替null
String sql="insert into txt (name,password) values ('"+name+"','"+password+"')"//你使用的表是txt,sql建表自己看着办吧
Statement stmt=con.createStatement()
if{
stmt.executeUpdate(sql)
response.sendRedirect("success.html")//根据结果定向成功页面
}else{
response.sendRedirect("f.html")//失败页面
}
}catch(Exception e){
e.printStackTrace()
System.out.println(e)
}
%>
</body>
</html>
至于success.jsp和f.jsp比较简单自己写下吧。
不会了可以上网查资料,或许再提问吧
哎 我给你最简单的例子两个简单的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+"/"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'My.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Hp.jsp">
name:<input name="name" value="" type="text"></br>
password:<input name="password" value="" type="text"></br>
<input type="submit" value="button">
</form>
</body>
</html>
处理页面 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+"/"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Hp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
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()
首先是数据库连接代码类:然后在你的jsp页面写上调用数据连接类的增删改查就可以了。
不懂hi我
jsp页面中
<%
String sqlgetServiceId="select e.id from eip_service e where e.service_name_en='"+serviceName.substring(serviceName.lastIndexOf("_")+1)+"' and e.service_version=1.0"
int sid=BaseDB.queryId(sqlgetServiceId, null)
%>
BaseDB.java
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.util.regex.Matcher
import java.util.regex.Pattern
public class BaseDB {
public static String URL = "jdbc:oracle:thin:@192.168.174.189:1521:soadb" //版本管理ERP数据库配置
public static String NAME = "SVMDEV"//用户名
public static String PWD = "SVMPWD"//密码
public static PreparedStatement ps =null
public static ResultSet rs =null
public static Connection connection=null
//获取数据库连接信息
public static Connection getConnection() {
try {
Class.forName("oracle.jdbc.OracleDriver")
if (connection==null) {
connection=DriverManager.getConnection(URL, NAME, PWD)
}
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
return connection
}
//查询数据,根据相关信息查询得到当前服务的某个需要的id
public static int queryId(String sql,String parameter[] ) {
int getId=0
try {
connection=getConnection()
ps=connection.prepareStatement(sql)
if (parameter!=null) {
for (int i = 1i <=parameter.lengthi++) {
ps.setString(i,parameter[i-1])
}
}
rs=ps.executeQuery()
if(rs.next()&&rs!=null){
getId=rs.getInt(1)
}
} catch (SQLException e) {
e.printStackTrace()
}finally{
closeAll(ps, rs, connection)
}
return getId
}
//修改数据
public static int updateData(String sql,String parameter[] ) {
int count=0
try {
connection=getConnection()
ps=connection.prepareStatement(sql)
if (parameter!=null) {
for (int i = 1i <=parameter.lengthi++) {
ps.setString(i,parameter[i-1])
}
}
count=ps.executeUpdate()
} catch (SQLException e) {
e.printStackTrace()
}finally{
closeAll(ps, rs, connection)
}
return count
}
//插入数据
public static int insertData(String sql,String parameter[]) {
int num=0
try {
connection=getConnection()
ps=connection.prepareStatement(sql)
if (parameter!=null) {
for (int i = 0i <parameter.lengthi++) {
ps.setString(i+1,parameter[i])
}
}
num=ps.executeUpdate()
} catch (Exception e) {
e.printStackTrace()
}finally{
closeAll(ps,null,connection)
}
return num
}
//关闭所有
public static void closeAll(PreparedStatement ps,ResultSet rs,Connection connection) {
try {
if (ps!=null) {
ps.close()
}
} catch (Exception e2) {
try {
if (rs!=null) {
rs.close()
rs=null
}
} catch (Exception e3) {
try {
if (connection!=null) {
//connection.close()
//connection=null
}
} catch (Exception e4) {
e4.printStackTrace()
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)