java 在jsp页面直接执行sql
就是用java在jsp页面连接数据库一个文本框输入sql语句一个按钮点击直接执行sql语句并把结果返回到Jsp页面中...
就是用java 在jsp页面连接数据库 一个文本框输入sql语句 一个按钮点击 直接执行sql语句 并把结果返回到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();
%>
</body>
</html>
注意 连接不同数据库要导入不同的数据库驱动包 你要导入才行啊
附 sqlserver数据库连接 部分关键代码:
private static Connection con = null;
private static Statement stm = null;
private static String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名称";
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//与mysql有所不同
private static String username = "sa";//默认用户
private static String pwd = "123"; //密码
static {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, pwd);
System.out.print("连接成功!");
stm = con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
首先是 获取值页面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();
%>
</body>
</html>
注意 连接不同数据库要导入不同的数据库驱动包 你要导入才行啊
附 sqlserver数据库连接 部分关键代码:
private static Connection con = null;
private static Statement stm = null;
private static String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名称";
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//与mysql有所不同
private static String username = "sa";//默认用户
private static String pwd = "123"; //密码
static {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, pwd);
System.out.print("连接成功!");
stm = con.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
展开全部
你要达到这种效果也很容易的。一个按钮点击,你通过ajax 或者 spring注解。触发java方法即可。页面上看起来就是你要的效果了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请使用jsp sql标签。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询