JavaScript收集的两个小代码!
时间:2007-02-17 来源:PHP爱好者
简单的数据库操作: <HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language=javascript>
function connectDb()
{
var ConnDB = new ActiveXObject("ADODB.Connection");
ConnDB.ConnectionString="dsn=shbxdatabase;uid=ldbx;pwd=123456;";
ConnDB.Open();
var Rs = new ActiveXObject("ADODB.Recordset");
var tempstr="";
Rs.Open("select * from WDA1",ConnDB,1,3);
while(!Rs.EOF)
{
tempstr = tempstr + Rs("TNAME") + "<br/>";
Rs.MoveNext;
}
test.innerHTML=tempstr;
}
</script>
</HEAD>
<BODY onbeforeunload="return 'ok?'"> <P><div id=test> </div>
<P><INPUT id=button1 type=button value=显示 name=button1 onclick="connectDb()"></P> </BODY>
</HTML>
>>>>>>------------------------------------- var db="D:js9tdb1.mdb";
function opendb(dbname,sql)
{
var opendb =new ActiveXObject("ADODB.Recordset")
opendb.ActiveConnection = "DBQ="+dbname+";DRIVER={Microsoft Access Driver (*.mdb)};"
opendb.Source = sql
opendb.CursorType = 1
opendb.CursorLocation = 2
opendb.LockType = 3
opendb.Open()
return opendb
}
var rs=new Object();
rs=opendb(db,"select * from 产品 where id="+i);
document.all.xx1.innerHTML=rs("生产厂商").value;
document.all.xx2.innerHTML=rs("产品名称")+"/"+rs("型号");
document.all.xx3.innerHTML=rs("售价");
document.all.xx4.innerHTML=rs("批发");
document.all.xx5.innerHTML=rs("备注");
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---------------------------------------
连接ACCESS只能用数据源,操作数据库的方法跟ASP之类是一样的。
只不过执行的语句不一样罢了。
如:
String dbUrl = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbConStr = "jdbc:odbc:dnsName";
Connection Conn = null;
Statement stmt = null;
try{
Class.forName(dbUrl);
}catch(ClassNotFoundException e){
System.err.println("Not Found ClassForName" + e.getMessage());
}
try{
Conn = DriverManager.getConnection(dbConStr, "uid", "pwd"); //连接数据库,uid与pwd分别为 用户名及密码(连接数据库的),如果没有既为空
stmt = Conn.createStatement();
}catch(Exception e){
System.err.println("Can't Connect the DATA" + e.getMessage());
}
//以下为操作数据库的
String sql = "select * from tableName[....]";
ResultSet Rs = stmt.executeQuery(sql); // 返回记录集
String sqlUpdate = "update...";
String sqlInsert = "insert into....";
stmt.executeUpdate(sqlUpdate); //更新记录
stmt.executeUpdate(sqlInsert); //插入记录
Conn.close();
stmt.close();
以上就是简单的数据库操作!
----------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language=javascript>
function connectDb()
{
var ConnDB = new ActiveXObject("ADODB.Connection");
ConnDB.ConnectionString="dsn=shbxdatabase;uid=ldbx;pwd=123456;";
ConnDB.Open();
var Rs = new ActiveXObject("ADODB.Recordset");
var tempstr="";
Rs.Open("select * from WDA1",ConnDB,1,3);
while(!Rs.EOF)
{
tempstr = tempstr + Rs("TNAME") + "<br/>";
Rs.MoveNext;
}
test.innerHTML=tempstr;
}
</script>
</HEAD>
<BODY onbeforeunload="return 'ok?'"> <P><div id=test> </div>
<P><INPUT id=button1 type=button value=显示 name=button1 onclick="connectDb()"></P> </BODY>
</HTML>
>>>>>>------------------------------------- var db="D:js9tdb1.mdb";
function opendb(dbname,sql)
{
var opendb =new ActiveXObject("ADODB.Recordset")
opendb.ActiveConnection = "DBQ="+dbname+";DRIVER={Microsoft Access Driver (*.mdb)};"
opendb.Source = sql
opendb.CursorType = 1
opendb.CursorLocation = 2
opendb.LockType = 3
opendb.Open()
return opendb
}
var rs=new Object();
rs=opendb(db,"select * from 产品 where id="+i);
document.all.xx1.innerHTML=rs("生产厂商").value;
document.all.xx2.innerHTML=rs("产品名称")+"/"+rs("型号");
document.all.xx3.innerHTML=rs("售价");
document.all.xx4.innerHTML=rs("批发");
document.all.xx5.innerHTML=rs("备注");
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---------------------------------------
连接ACCESS只能用数据源,操作数据库的方法跟ASP之类是一样的。
只不过执行的语句不一样罢了。
如:
String dbUrl = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbConStr = "jdbc:odbc:dnsName";
Connection Conn = null;
Statement stmt = null;
try{
Class.forName(dbUrl);
}catch(ClassNotFoundException e){
System.err.println("Not Found ClassForName" + e.getMessage());
}
try{
Conn = DriverManager.getConnection(dbConStr, "uid", "pwd"); //连接数据库,uid与pwd分别为 用户名及密码(连接数据库的),如果没有既为空
stmt = Conn.createStatement();
}catch(Exception e){
System.err.println("Can't Connect the DATA" + e.getMessage());
}
//以下为操作数据库的
String sql = "select * from tableName[....]";
ResultSet Rs = stmt.executeQuery(sql); // 返回记录集
String sqlUpdate = "update...";
String sqlInsert = "insert into....";
stmt.executeUpdate(sqlUpdate); //更新记录
stmt.executeUpdate(sqlInsert); //插入记录
Conn.close();
stmt.close();
以上就是简单的数据库操作!
----------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
相关阅读 更多 +