4个回答
2013-08-16
展开全部
网上的,可以参考一下
<%
’by dicky 2005-03-22 21:52:18 am qq:25941 e-mail:applebbs@gmail.com
const issql = 1 ’定义数据库类型为sql server
call openconn(conn) ’打开数据库连接
dim rs,sql
set rs = server.createobject("adodb.recordset")
sql = "select * from products order by productname"
rs.open sql,conn,1,1 ’以只读方式查询数据记录
if rs.eof then
response.write "sorry,no record!" ’如果没有记录
else
dim objxmldom, objrootnode, objnode
set objxmldom = server.createobject("msxml2.domdocument") ’创建xml文件对象
set objrootnode = objxmldom.createelement("xml") ’创建根节点
objxmldom.documentelement = objrootnode
do while not rs.eof ’循环出所有记录
’ response.write rs("productname") &"<br>"
set objrownode = objxmldom.createelement("row") ’创建父节点
set objnode = objxmldom.createelement("productname") ’创建子节点
objnode.text = rs("productname")
objrownode.appendchild(objnode)
set objnode = objxmldom.createelement("unitprice")
objnode.text = rs("unitprice")
objrownode.appendchild(objnode)
set objnode = objxmldom.createelement("unitsinstock")
objnode.text = rs("unitsinstock")
objrownode.appendchild(objnode)
objrootnode.appendchild(objrownode)
rs.movenext:loop ’循环结束
objxmldom.save "d:\myxmldoc.xml" ’写入xml文件 能用变量让用户在页面上自定义文件名
response.write "<script>alert(’恭喜,写入xml文件成功!’);</script>"
set objnode = nothing ’销毁对象
set objrownode = nothing ’销毁对象
set objrootnode = nothing ’销毁对象
end if
rs.close
set rs = nothing
call closeconn() ’关闭数据库连接
function openconn(conn) ’打开数据库连接
dim connstr
if issql = 1 then ’如果是sql server数据库
’sql server数据库连接参数:用户名、用户密码、数据库名、连接名(本地用local,外地用ip)
dim sqlusername,sqlpassword,sqldatabasename,sqllocalname
sqlusername = "sa"
sqlpassword = ""
sqldatabasename = "northwind"
sqllocalname = "(local)"
connstr = "provider = sqloledb; user id = " & sqlusername & "; password = " & sqlpassword & "; initial catalog = " & sqldatabasename & "; data source = " & sqllocalname & ";"
else ’如果是access数据库
dim db
’第一次使用请修改本处数据库地址并相应修改数据库名称,如将dicky.mdb修改为dicky.asp(防止恶意下载access数据库)
db = "dicky.mdb"
connstr = "provider = microsoft.jet.oledb.4.0;data source = " & server.mappath(db)
end if
on error resume next
set conn = server.createobject("adodb.connection")
conn.open connstr
if err then
’ err.clear
set conn = nothing
response.write "数据库连接出错,请检查连接字串。"
response.end
end if
set rs = server.createobject("adodb.recordset")
end function
function closeconn() ’关闭数据库连接
conn.close
set conn = nothing
end function
%>
<%
’by dicky 2005-03-22 21:52:18 am qq:25941 e-mail:applebbs@gmail.com
const issql = 1 ’定义数据库类型为sql server
call openconn(conn) ’打开数据库连接
dim rs,sql
set rs = server.createobject("adodb.recordset")
sql = "select * from products order by productname"
rs.open sql,conn,1,1 ’以只读方式查询数据记录
if rs.eof then
response.write "sorry,no record!" ’如果没有记录
else
dim objxmldom, objrootnode, objnode
set objxmldom = server.createobject("msxml2.domdocument") ’创建xml文件对象
set objrootnode = objxmldom.createelement("xml") ’创建根节点
objxmldom.documentelement = objrootnode
do while not rs.eof ’循环出所有记录
’ response.write rs("productname") &"<br>"
set objrownode = objxmldom.createelement("row") ’创建父节点
set objnode = objxmldom.createelement("productname") ’创建子节点
objnode.text = rs("productname")
objrownode.appendchild(objnode)
set objnode = objxmldom.createelement("unitprice")
objnode.text = rs("unitprice")
objrownode.appendchild(objnode)
set objnode = objxmldom.createelement("unitsinstock")
objnode.text = rs("unitsinstock")
objrownode.appendchild(objnode)
objrootnode.appendchild(objrownode)
rs.movenext:loop ’循环结束
objxmldom.save "d:\myxmldoc.xml" ’写入xml文件 能用变量让用户在页面上自定义文件名
response.write "<script>alert(’恭喜,写入xml文件成功!’);</script>"
set objnode = nothing ’销毁对象
set objrownode = nothing ’销毁对象
set objrootnode = nothing ’销毁对象
end if
rs.close
set rs = nothing
call closeconn() ’关闭数据库连接
function openconn(conn) ’打开数据库连接
dim connstr
if issql = 1 then ’如果是sql server数据库
’sql server数据库连接参数:用户名、用户密码、数据库名、连接名(本地用local,外地用ip)
dim sqlusername,sqlpassword,sqldatabasename,sqllocalname
sqlusername = "sa"
sqlpassword = ""
sqldatabasename = "northwind"
sqllocalname = "(local)"
connstr = "provider = sqloledb; user id = " & sqlusername & "; password = " & sqlpassword & "; initial catalog = " & sqldatabasename & "; data source = " & sqllocalname & ";"
else ’如果是access数据库
dim db
’第一次使用请修改本处数据库地址并相应修改数据库名称,如将dicky.mdb修改为dicky.asp(防止恶意下载access数据库)
db = "dicky.mdb"
connstr = "provider = microsoft.jet.oledb.4.0;data source = " & server.mappath(db)
end if
on error resume next
set conn = server.createobject("adodb.connection")
conn.open connstr
if err then
’ err.clear
set conn = nothing
response.write "数据库连接出错,请检查连接字串。"
response.end
end if
set rs = server.createobject("adodb.recordset")
end function
function closeconn() ’关闭数据库连接
conn.close
set conn = nothing
end function
%>
2013-08-16
展开全部
用DataAdapter填到DataSet
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-16
展开全部
//ConToSql.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author as sasa @ version 1.0 @ serial
*/
public class ConToSql {
/**
*
*/
Connection conn;
Statement st;
ResultSet rs, rs1;
public ConToSql(String tableName, String userName, String password) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="
+ tableName;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url, userName, password);
st = conn.createStatement();
} catch (Exception e) {
System.out.print("连接数据库出错了" + e.toString());
e.printStackTrace();// TODO: handle exception
}
}
public Connection getConnect() {
return conn;
}
public Statement getStatement() {
return st;
}
public void conToSqlClose() {
if (st != null) {
try {
st.close();
} catch (Exception e) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ResultSet rs;
String sql = "select * from student";
ConToSql thisClass = new ConToSql("CampusIP", "sa", "");
Statement st = thisClass.getStatement();
try {
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.println("name:" + rs.getString(1));
System.out.println("sex:" + rs.getString(2));
System.out.println("age:" + rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
thisClass.conToSqlClose();
}
}
}
//DataToXml.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataToXml {
ConToSql contosql;
Statement st;
ResultSet rs;
private String strXmlFileName;
public DataToXml() {
strXmlFileName = new String();
contosql = new ConToSql("CampusIP", "sa", "");
}
private void createXml(String strXml) {
String SQL = "select * from student";
strXmlFileName = strXml;
String[] strTemp = new String[3];
try {
st = contosql.getStatement();
rs = st.executeQuery(SQL);
BufferedWriter outXml = new BufferedWriter(new FileWriter(
strXmlFileName));
outXml.write("<?xml version= \"1.0\" encoding=\"gb2312\"?>");
outXml.newLine();
outXml.write("<people>");
while (rs.next()) {
strTemp[0] = rs.getString(1);
strTemp[1] = rs.getString(2);
strTemp[2] = "" + rs.getInt(3);
outXml.newLine();
outXml.write(" <student>");
outXml.newLine();
outXml.write(" <name>" + strTemp[0] + "</name>");
outXml.newLine();
outXml.write(" <sex>" + strTemp[1] + "</sex>");
outXml.newLine();
outXml.write(" <age>" + strTemp[2] + "</age>");
outXml.newLine();
outXml.write(" </student>");
}
outXml.newLine();
outXml.write("</people>");
outXml.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
contosql.conToSqlClose();
}
}
public static void main(String[] args) {
String xmlName = "student.xml";
DataToXml thisClass = new DataToXml();
thisClass.createXml(xmlName);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author as sasa @ version 1.0 @ serial
*/
public class ConToSql {
/**
*
*/
Connection conn;
Statement st;
ResultSet rs, rs1;
public ConToSql(String tableName, String userName, String password) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="
+ tableName;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url, userName, password);
st = conn.createStatement();
} catch (Exception e) {
System.out.print("连接数据库出错了" + e.toString());
e.printStackTrace();// TODO: handle exception
}
}
public Connection getConnect() {
return conn;
}
public Statement getStatement() {
return st;
}
public void conToSqlClose() {
if (st != null) {
try {
st.close();
} catch (Exception e) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ResultSet rs;
String sql = "select * from student";
ConToSql thisClass = new ConToSql("CampusIP", "sa", "");
Statement st = thisClass.getStatement();
try {
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.println("name:" + rs.getString(1));
System.out.println("sex:" + rs.getString(2));
System.out.println("age:" + rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
thisClass.conToSqlClose();
}
}
}
//DataToXml.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataToXml {
ConToSql contosql;
Statement st;
ResultSet rs;
private String strXmlFileName;
public DataToXml() {
strXmlFileName = new String();
contosql = new ConToSql("CampusIP", "sa", "");
}
private void createXml(String strXml) {
String SQL = "select * from student";
strXmlFileName = strXml;
String[] strTemp = new String[3];
try {
st = contosql.getStatement();
rs = st.executeQuery(SQL);
BufferedWriter outXml = new BufferedWriter(new FileWriter(
strXmlFileName));
outXml.write("<?xml version= \"1.0\" encoding=\"gb2312\"?>");
outXml.newLine();
outXml.write("<people>");
while (rs.next()) {
strTemp[0] = rs.getString(1);
strTemp[1] = rs.getString(2);
strTemp[2] = "" + rs.getInt(3);
outXml.newLine();
outXml.write(" <student>");
outXml.newLine();
outXml.write(" <name>" + strTemp[0] + "</name>");
outXml.newLine();
outXml.write(" <sex>" + strTemp[1] + "</sex>");
outXml.newLine();
outXml.write(" <age>" + strTemp[2] + "</age>");
outXml.newLine();
outXml.write(" </student>");
}
outXml.newLine();
outXml.write("</people>");
outXml.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
contosql.conToSqlClose();
}
}
public static void main(String[] args) {
String xmlName = "student.xml";
DataToXml thisClass = new DataToXml();
thisClass.createXml(xmlName);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-16
展开全部
//ConToSql.java
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询