Recently in the review of Ajax technology, we picked up the same year, "Ajax classic Case Development Daquan" to brush up. Found that there are some errors, it is pointed out now, this can help after the scholar less detours. The main technologies are: MYSQL, JDBC, JSON, Ajax, JSP. In fact, this article is not original, the main content is excerpted from the "Ajax classic case development Daquan."
1. Database design
Drop database if exists mydb;
Create DATABASE mydb character Set GBK;
-Multi-level linkage menu use mydb;
drop table if exists select_menu;
CREATE TABLE Select_menu (ID varchar (255) NOT NULL default ", text varchar (255) NOT NULL, PID varchar (255) is not NULL,
Seq Int (one) not NULL default 0, PRIMARY key (ID)) Engine=innodb default CHARSET=GBK;
INSERT into Select_menu values (' A1 ', ' List A option 1 ', ' INIT ', 1);
INSERT into Select_menu values (' A2 ', ' List A option 2 ', ' INIT ', 2);
INSERT into Select_menu values (' B11 ', ' list B ', ' A1 ', 1);
INSERT into Select_menu values (' B12 ', ' list B ' option, ' A1 ', 2);
INSERT into Select_menu values (' B13 ', ' List B options ', ' A1 ', 3);
INSERT into Select_menu values (' B21 ', ' List B options ', ' A2 ', 1);
INSERT into Select_menu values (' B22 ', ' List B options ', ' A2 ', 2);
INSERT into Select_menu values (' C111 ', ' list C ' option 111 ', ' B11 ', 1);
INSERT into Select_menu values (' C112 ', ' list C options ', ' B11 ', 2);
INSERT into Select_menu values (' C121 ', ' list C ' option 121 ', ' B12 ', 1);
INSERT into Select_menu values (' C122 ', ' list C ' option 122 ', ' B12 ', 2); INSERT into Select_menu values (' C131 ', 'List C option 131 ', ' B13 ', 1);
INSERT into Select_menu values (' C132 ', ' list C option ', ' B13 ', 2);
INSERT into Select_menu values (' C211 ', ' list C ' option 211 ', ' B21 ', 1);
INSERT into Select_menu values (' C212 ', ' list C ' option 212 ', ' B21 ', 2);
INSERT into Select_menu values (' C221 ', ' list C ' option 221 ', ' B22 ', 1); INSERT into Select_menu values (' C222 ', ' list C ' option 222 ', ' B22 ', 2);
2. Tool classes to connect to the database
Package com.lanp.ajax.db;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException; /** * A tool class connected to a database, defined as non-inheritable and private access * @author LANP * @since 2012-2-29 22:27 */Public final class Dbutils {private static
String url = "JDBC:MYSQL://LOCALHOST:3306/MYDB?CHARACTERENCODING=GBK";
private static String user = "root";
private static String PSW = "root";
private static Connection Conn;
static {try {Class.forName ("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {e.printstacktrace ();
throw new RuntimeException (e);
}} private Dbutils () {}/** * get connection to database * @return Conn */public static Connection getconnection () {
try {conn = drivermanager.getconnection (URL, user, PSW);
} catch (SQLException e) {e.printstacktrace ();
throw new RuntimeException (e);
} return conn; /** * Release RESOURCES * @param conn * @param pstmt * @param rs
*/public static void Closeresources (Connection conn,preparedstatement pstmt,resultset rs) {if (null! = rs) {try
{Rs.close ();
} catch (SQLException e) {e.printstacktrace ();
throw new RuntimeException (e);
} finally {if (null! = pstmt) {try {pstmt.close ();
} catch (SQLException e) {e.printstacktrace ();
throw new RuntimeException (e);
} finally {if (null! = conn) {try {conn.close ();
} catch (SQLException e) {e.printstacktrace ();
throw new RuntimeException (e);
}
}
}
}
}
}
}
}
3.select_menu.html page
<!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
4.select_menu.jsp Backstage Service
<%@ page language= "java" contenttype= "text/html;
Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ page import=" java.sql.*,com.lanp.ajax.db.dbutils "%> <%!
Accessing the database Gets the subordinate option information string getoptions (string selectedid) {int counter = 0;
StringBuffer opts = new StringBuffer ("{"); String sql = "SELECT * from Select_menu where pid=?"
ORDER by seq ASC ";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {conn = dbutils.getconnection ();
pstmt = conn.preparestatement (sql);
Pstmt.setstring (1,selectedid);
rs = Pstmt.executequery ();
while (Rs.next ()) {//If it is not the first item, append a "," to delimit the option if (Counter > 0) {opts.append (",");
} opts.append ("'");
Opts.append (rs.getstring ("id"));
Opts.append ("': '");
Opts.append (rs.getstring ("text"));
Opts.append ("'");
counter++;
}} catch (SQLException e) {System.out.println (e.tostring ());
} finally {dbutils.closeresources (CONN,PSTMT,RS); } Opts.apPend ("}");
System.out.println (Opts.tostring ());
return opts.tostring ();
}%> <% out.clear ();
String Selectedid = Request.getparameter ("Selectedid");
String optionsinfo = getoptions (Selectedid);
Out.print (Optionsinfo); %>
Ok,tks.