这是一个我们搞web开发中的常见的问题:下拉列表+JAVAScript,问题描述:用户打开一个页面,页面有一个表单,表单由一个下拉列表、一个文本框,一按钮。下拉列的框的值是动态加载,如果用户对下拉列表的值不满意,可以在文本框输入一个值,单击按钮,此时下拉列表中又多了一个值。开发环境:Myeclipse6.0+JDK1.6+Tomcat5.0
一、首先建立好与数据库连拉:
package com.core;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class ConnDB {
??? ? public Connection conn = null;
??? ? public Statement stmt = null;
??? ? public ResultSet rs = null;
private static String propFileName = "/connDB.properties"; //指定资源文件保存的位置
??? ? private static Properties prop = new Properties();
??? ? private static String dbClassName ="com.mysql.jdbc.Driver";
??? ? private static String dbUrl =
??? ????? "jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=root&useUnicode=true&characterEncoding=GBK";
??? ? public ConnDB(){
??? ??? try {
??? ????? InputStream in=getClass().getResourceAsStream(propFileName);
??? ????? prop.load(in); //通过输入流对象加载Properties文件
??? ????? dbClassName = prop.getProperty("DB_CLASS_NAME");//获取数据库驱动
??? ????? dbUrl = prop.getProperty("DB_URL",
??? ???????????????????????????? ??"jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=root&useUnicode=true&characterEncoding=GBK");
??? ??? }
??? ??? catch (Exception e) {
??? ????? e.printStackTrace();//输出异常信息
??? ??? }
??? ? }
??? ? public static Connection getConnection() {
??? ??? Connection conn = null;
??? ??? try {
??? ????? Class.forName(dbClassName).newInstance();
??? ????? conn = DriverManager.getConnection(dbUrl);
??? ??? }
??? ??? catch (Exception ee) {
??? ????? ee.printStackTrace();
??? ??? }
??? ??? if (conn == null) {
??? ????? System.err.println(
??? ????????? "警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
??? ????????? dbClassName + "\r\n链接位置:" + dbUrl);
??? ??? }
??? ??? return conn;
??? ? }
??? /*
??? ?* 功能:执行查询语句
??? ?*/
??? public ResultSet executeQuery(String sql) {
??????? try {
??????????? conn = getConnection();
??????????? stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
??????????????????? ResultSet.CONCUR_READ_ONLY);
??????????? rs = stmt.executeQuery(sql);
??????? } catch (SQLException ex) {
??????????? System.err.println(ex.getMessage());
??????? }
??????? return rs;
??? }
??? /*
??? ?* 功能:执行更新操作
??? ?*/
??? public int executeUpdate(String sql) {
??????? int result = 0;
??????? try {
??????????? conn = getConnection(); //调用getConnection()方法构造Connection对象的一个实例conn
??????????? stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
??????????????????? ResultSet.CONCUR_READ_ONLY);
??????????? result = stmt.executeUpdate(sql);?? //执行更新操作
??????? } catch (SQLException ex) {
??????????? result = 0;
??????? }
??????? return result;
??? }
??? /*
??? ?* 功能:关闭数据库的连接
??? ?*/
??? public void close() {
??????? try {
??????????? if (rs != null) {
??????????????? rs.close();
??????????? }
??????????? if (stmt != null) {
??????????????? stmt.close();
??????????? }
??????????? if (conn != null) {
??????????????? conn.close();
??????????? }
??????? } catch (Exception e) {
??????????? e.printStackTrace(System.err);
??????? }
??? }
在src下建一属性文件:connDB.properties
#DB_CLASS_NAME
DB_CLASS_NAME=com.mysql.jdbc.Driver
?
#DB_URL
DB_URL=jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=root&useUnicode=true&characterEncoding=GBK
二、建立数据库脚本:
CREATE TABLE `times` (
? `ti` int(5) NOT NULL,
? PRIMARY KEY? (`ti`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
Insert into times values (2),(4),(6),(8),(10),(12),(14),(16);