|
本人名称:jsp+ajax技术实现行政区划代码三级关联下拉框代码
package untitled1;
import java.io.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class DatabaseOp {
//声明数据库jdbc驱动变量
static String jdbcClass = "oracle.jdbc.driver.OracleDriver";
//声明数据库url变量
static String jdbcURL = "jdbc:oracle:thin:@127.0.0.1:1521:databasename";//注意数据库名
static String userName = ""; //修改为你自己的用户密码
static String password = "";
//创建执行sql语句的对象变量
public Connection conn;
Statement stmt;
public DatabaseOp() {
}
public String getcanton() {
String RS = null;
int i = 0;
getCon(userName, password);
ResultSet ds = null;
try {
if (stmt != null) {
String SqlText = "select c.cantoncode,c.cantonname from CHs_Canton c where c.pcantoncode is null order by c.cantonname";
ds = stmt.executeQuery(SqlText);
RS = "";
while (ds.next()) {
RS = RS + "<option value=\"" + ds.getString(1) + "\""; //从1开始的
if (i == 0) {
RS = RS + " selected ";
}
RS = RS + ">" + ds.getString(2) + "</option>";
i++;
}
RS = RS + "\r";
}
}
catch (Exception e)
{
RS = e.toString() + "错误";
}
finally {
try {
stmt.close();
}
catch (Exception e)
{
}
try {
conn.close();
}
catch (Exception e)
{
}
}
return RS;
}
public String getcanton(String pCantonCode) {
String RS = null;
int i = 0;
ResultSet ds = null;
getCon(userName, password);
try {
if (stmt != null) {
String SqlText =
"select c.cantoncode,c.cantonname from CHs_Canton c where c.pcantoncode='" +
pCantonCode + "' order by c.cantonname";
ds = stmt.executeQuery(SqlText);
RS = "";
while (ds.next()) {
RS = RS + ds.getString(1) + "," + ds.getString(2) + "$"; //从1开始的
}
}
}
catch (Exception e)
{
RS = e.toString() + "错误";
}
finally {
try {
stmt.close();
}
catch (Exception e)
{
}
try {
conn.close();
}
catch (Exception e)
{
}
}
return RS;
}
public boolean getCon(String UserName, String PassWord) {
try {
//加载数据库驱动程序
Class.forName(jdbcClass);
//建立数据库连接
conn = DriverManager.getConnection(jdbcURL, UserName, PassWord);
stmt = conn.createStatement();
return true;
}
catch (Exception e) {
e.printStackTrace();
stmt = null;
return false;
}
}
}
共2页: 上一页 1 [2] 下一页
|