利用Javascript、servlet和ajax实现SQL数据库访问
以前设计的要访问数据库页面表单选择后都是先通过提交后,再由服务器返回新的过滤页面,相当于刷新了页面,这样此前表单中已填列的内容会清空,必须重新输入,很不方便。
上海满智的Emsflowjava连接sqlserver数据库开发平台做得很好,但他们的源代码不开放,所以离开了他们的平台很多功能就没法实现。
现在通过Javascript、servlet和ajax,能实现数据库的访问,要通过访问数据库过滤表单内容的,只用在客户端页面的Javascript中实现,自动从后台获取服务器中的数据库内容,并填入表单中,相当方便适用。测试的三个程序代码如下,直接可以应用,但在实际开发中,可以比照着修改一下,数据库连接可以改用连接池,SQL的查询条件语句可以用String condition += sql ;表示:
一、js_ajax.jsp 代码:
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml">
<head>
<title>JS+ajax访问数据库</title>
</head>
<script src="jqueryUI_1_8/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script language = "javascript">
function checkValue()
{
var buttonValue = ElementById("textfield").value;
if(buttonValue.length == 0)
{
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml">
<head>
<title>JS+ajax访问数据库</title>
</head>
<script src="jqueryUI_1_8/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script language = "javascript">
function checkValue()
{
var buttonValue = ElementById("textfield").value;
if(buttonValue.length == 0)
{
alert("输入不能为空!")
ElementById("textfield").focus();
}
else
{
if(buttonValue.length==11)
{
alert("手机的位数应为11位!");
ElementById("textfield").value = "";
ElementById("textfield").focus();
}
else
{
$.ajax(
{
ElementById("textfield").focus();
}
else
{
if(buttonValue.length==11)
{
alert("手机的位数应为11位!");
ElementById("textfield").value = "";
ElementById("textfield").focus();
}
else
{
$.ajax(
{
//method:"get",
type:"post",
url:"./jsajax",
data:{buttonValue: String()},
success:function(data)
{
&ElementById("textdata").value=data;
if(data=="false")
{
alert("错误:您的号[" + String() + "]未在系统中注册。");
}
else
{
//提供更改密码服务
type:"post",
url:"./jsajax",
data:{buttonValue: String()},
success:function(data)
{
&ElementById("textdata").value=data;
if(data=="false")
{
alert("错误:您的号[" + String() + "]未在系统中注册。");
}
else
{
//提供更改密码服务
alert(data);
}
}
});
}
}
}
</script>
}
}
});
}
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="">
<label>ID号
<input type="text" id="textfield" name="textfield" />
姓名<input type="text" id="textdata" name="textdata" />
</label>
<form id="form1" name="form1" method="post" action="">
<label>ID号
<input type="text" id="textfield" name="textfield" />
姓名<input type="text" id="textdata" name="textdata" />
</label>
<p> </p>
<p>
<input type="button" value="显示姓名" title="显示姓名" onClick="checkValue()"/>
<input type="button" value="返回" title="返回" onClick="window.location.href='js_ajax.jsp';"/>
</p>
</form>
</body>
</html>
<p>
<input type="button" value="显示姓名" title="显示姓名" onClick="checkValue()"/>
<input type="button" value="返回" title="返回" onClick="window.location.href='js_ajax.jsp';"/>
</p>
</form>
</body>
</html>
二、 /com/jsajax/validate.java 代码:
package com.jsajax;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class validate extends HttpServlet
{
public validate()
{
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
response.setContentType("text/html;charset=GBK");
//response.setContentType("text/html");
import javax.servlet.*;
public class validate extends HttpServlet
{
public validate()
{
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
response.setContentType("text/html;charset=GBK");
//response.setContentType("text/html");
//PrintWriter out = Writer();
String value = Parameter("buttonValue");
//out.println(value);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shougou";
String password="123";
String user="jyz";
String data="";
java.sql.Connection connection = java.Connection(url,user,password);
java.sql.Statement statement = ateStatement();
String sql = "select * from d_denglu where dl_id = '" + value + "'";
java.sql.ResultSet rs = uteQuery(sql);
if (rs.next())
{
String value = Parameter("buttonValue");
//out.println(value);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shougou";
String password="123";
String user="jyz";
String data="";
java.sql.Connection connection = java.Connection(url,user,password);
java.sql.Statement statement = ateStatement();
String sql = "select * from d_denglu where dl_id = '" + value + "'";
java.sql.ResultSet rs = uteQuery(sql);
if (rs.next())
{
String("dl_mincheng");
// data = new Bytes("ISO-8859-1"),"utf-8");
Writer().write(data);
}
else
{Writer().write("false"); };
rs.close();rs=null;
statement.close();statement=null;
connection.close();connection=null;
}
catch (Exception e)
{
e.printStackTrace();
}
// data = new Bytes("ISO-8859-1"),"utf-8");
Writer().write(data);
}
else
{Writer().write("false"); };
rs.close();rs=null;
statement.close();statement=null;
connection.close();connection=null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
}
三、l 代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun/dtd/web-app_2_3.dtd">
<web-app version="2.4"
xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun/dtd/web-app_2_3.dtd">
<web-app version="2.4"
xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>validate</servlet-name>
<servlet-class>com.jsajax.validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validate</servlet-name>
<url-pattern>/jsajax</url-pattern>
</servlet-mapping>
</web-app>
java.sun/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>validate</servlet-name>
<servlet-class>com.jsajax.validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validate</servlet-name>
<url-pattern>/jsajax</url-pattern>
</servlet-mapping>
</web-app>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论