第一步: 建立mysql 数据库,创建noteuser表和增加1条信息
sql 代码
- CREATE TABLE `noteuser` (
- `userid` int(5) NOT NULL auto_increment,
- `username` varchar(16) character set utf8 default NULL,
- `password` varchar(16) character set utf8 default NULL,
- `regtime` datetime default NULL,
- `NickName` varchar(16) character set utf8 default NULL,
- `Sex` int(1) unsigned default NULL,
- `Email` varchar(15) character set utf8 default NULL,
- `age` int(2) unsigned default NULL,
- PRIMARY KEY (`userid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-
-
- INSERT INTO noteuser(username,password,regtime,NickName,Sex,Email,age) VALUES ('YuKy','11',now(),'Zoo.YuKy',1,'[email protected]',21);
- //可以添加更多信息
第二步:1.下载jsonrpc-1.0.jar , http://oss.metaparadigm.com/jsonrpc-dist/json-rpc-java-1.0.zip
2.在C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ 建立新文件夹json_Test,然后把json-rpc-java-1.0.zip里的jsonrpc.js放在...\webapps\json_Test\js的文件夹里面.在js文件夹里增加aa.js.
//--把下面的代码复制到aa.js
js 代码
- function clrscr()
- {
- resultNode.value = "";
- }
-
- function print(s)
- {
- resultNode.value += "" + s;
- resultNode.scrollTop = resultNode.scrollHeight;
- }
-
-
- function onLoad()
- {
- resultNode = document.getElementById("result");
-
- try {
- jsonrpc = new JSONRpcClient("JSON-RPC");
- } catch(e) {
- if(e.message) alert(e.message);
- else alert(e);
- }
- }
-
- function loginUser(){
- var user = document.getElementById("username").value;
- var pass = document.getElementById("password").value;
-
- if(user == null || pass == null ||user == "" || pass == ""){
- alert("Please enter username and password!");
- }else{
- var ss = jsonrpc.user.loginUser(user,pass);
- alert(ss);
- }
- }
-
- function dogetUserInfo(){
- clrscr();
- print("Running user\n\n");
- try {
- print("user.getUserInfo()");
- print(" returns " + jsonrpc.user.getUserInfo() + "\n");
- } catch(e) {
- print(" Exception: \n\n" + e);
- }
- }
第三步:编写JAVA程序,jsonTest.java
java 代码
- package com.metaparadigm.jsonrpc.test;
-
- import java.io.Serializable;
- import java.sql.*;
- public class jsonTest implements Serializable
- {
- private final static long serialVersionUID = 1;
- private String strSql;
- private ResultSet rs;
- private Statement stmt = null;
- private Connection conn = null;
-
- public Connection getConnection(){
- try{
- Class.forName("org.gjt.mm.mysql.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8","root","root");
- }catch(Exception ex){
- ex.printStackTrace();
- }
- return conn;
- }
-
- public String[] getUserInfo(){
- String[] row = null;
- try{
- strSql = "select username from noteuser";
- conn = getConnection();
- stmt = conn.createStatement();
- rs = stmt.executeQuery(strSql);
- int i = 0;
- while(rs.next()){
- i++;
- }
- System.out.println(i);
- rs = stmt.executeQuery(strSql);
- row = new String[i];
- i =0;
-
- while(rs.next()){
- row[i] = rs.getString(1);
- i++;
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- for(int j=0; j<row.length; j++)
- System.out.println(row[j]);
-
- return row;
- }
- }
第四步: 写JSP页面:cc.jsp
xml 代码
-
- <%@
- page contentType="text/html; charset=UTF-8" %><%@
- page language="java" %><%@
- page import="com.metaparadigm.jsonrpc.test.jsonTest"
- %>
- <jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
- <jsp:useBean id="user" scope="session" class="com.metaparadigm.jsonrpc.test.jsonTest"/>
- <%JSONRPCBridge.registerObject("user", user); %>
-
- <script language="javascript" type="text/javascript" src="js/jsonrpc.js"></script>
- <script type="text/javascript" src="js/aa.js"></script>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>JSON查询MYSQL</title>
- </head>
-
- <body onLoad="onLoad()">
- <p>请输入你的用户名字和密码</p>
- <p>用户名:
- <input type="text" id="username">
- </p>
- <p>密 码:
- <input type="password" id="password" >
- </p>
- <p>
- <input type="button" onClick="loginUser()" value="登陆" ></p>
-
- <textarea wrap="off" id="result" cols="80" rows="20"></textarea>
- <a href="javascript:dogetUserInfo();">查看</a>
-
- </body>
- </html>
第五步:配置WEB-INF(自己创建以下的文件夹,在这里不一一说明有什么作用)
1.添加web.xml,在..\webapps\json_Test\WEB-INF文件夹,在\WEB-INF\添加web.xml,把以下代码复制进去
xml 代码
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <servlet>
- <servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name>
- <servlet-class>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name>
- <url-pattern>/JSON-RPC</url-pattern>
- </servlet-mapping>
- </web-app>
2.添加jsonrpc-1.0.jar,把json-rpc-java-1.0.zip内的jsonrpc-1.0.jar复制到..\webapps\json_Test\WEB-INF\lib\里
3.添加jsonTest.class,把jsonTest.java编译好的class文件在..\webapps\json_Test\WEB-
INF\classes\com\metaparadigm\jsonrpc\test\文件夹里.
第六步:测试,运行Tomcat,在地址栏输入http://localhost:8080/json_Test/cc.jsp,然后输入用户名yuky,密码11,看是否能登陆成功.