JavaScript 操作数据库

简单的数据库操作:

<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>&nbsp;</div>
<P><INPUT id=button1 type=button value=显示 name=button1 onclick="connectDb()"></P>

</BODY>
</HTML>
>>>>>>-------------------------------------

var db="D:\\js\\9t\\db1.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("Cant 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();


以上就是简单的数据库操作!

你可能感兴趣的:(JavaScript,sql,jdbc,Access,asp)