1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib下.
2 加载JDBC驱动,并将其注册到DriverManager中,下面是一些主流数据库的JDBC驱动加裁注册的代码: //Oracle8/8i/9iO数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Sql Server7.0/20xx数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); //DB2数据库
Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
//Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
//MySQL数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
//PostgreSQL数据库
Class.forNaem("org.postgresql.Driver").newInstance();
3 建立数据库连接,取得Connection对象.例如:
//Oracle8/8i/9i数据库(thin模式)
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String password="tiger";
Connection conn=DriverManager.getConnection(url,user,password);
//Sql Server7.0/20xx数据库
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
//DB2数据库
String url="jdbc:db2://localhost:5000/sample";
String user="amdin"
String password=-"";
Connection conn=DriverManager.getConnection(url,user,password);
//Informix数据库
String url=
"jdbc:informix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";
Connection conn=DriverManager.getConnection(url);
//Sybase数据库
String url="jdbc:sybase:Tds:localhost:5007/tsdata";
Properties sysProps=System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn=DriverManager.getConnection(url,SysProps);
//MySQL数据库
String url=
"jdbc:mysql://localhost:3306/testDB?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
Connection conn=DriverManager.getConnection(url);
//PostgreSQL数据库
String url="jdbc:postgresql://localhost/testDB";
String user="myuser";
String password="mypassword"; Connection conn=DriverManager.getConnection(url,user,password);
4 建立Statement对象或PreparedStatement对象.例如:
//建立Statement对象
Statement stmt=conn.createStatement();
//建立ProparedStatement对象
String sql="select * from user where userName=? and password=?";
PreparedStatement pstmt=Conn.prepareStatement(sql);
pstmt.setString(1,"admin"); pstmt.setString(2,"liubin");
5 执行SQL语句.例如:
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//执行动态SQL查询
ResultSet rs=pstmt.executeQuery();
//执行insert update delete等语句,先定义sql stmt.executeUpdate(sql);
6 访问结果记录集ResultSet对象。例如:
while(rs.next)
{
out.println("你的第一个字段内容为:"+rs.getString());
out.println("你的第二个字段内容为:"+rs.getString(2));
} 7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源.例如:
rs.close();
stmt.clost();
pstmt.close();
con.close();
补充:
MySQL:
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name"; //连接的URL,db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password); Microsoft SQL Server 2.0驱动(3个jar的那个):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); // Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); // Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名 String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password); Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password); PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名 String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password); DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名 String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password); Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password); JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
第二篇:ASP常用连接数据库语法总结
ASP常用连接数据库语法总结
<一>数据库的连接方法:
1.Access数据库的DSN-less连接方法:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("数据库所在路径")
2.Access OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("数据库所在路径")
3.SQL server连接方法:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=数据库名;"
4.SQL server OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=数据库名"
5.Oracle 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft
oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
7.dBase 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
8.mySQL 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;
uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro 连接方法: odbc for
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};
sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
10.MS text 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
11.MS text OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
<二>常用的四种SQL命令:
1.查询数据记录(Select)
语法:Select 字段串行 From table Where 字段=内容
例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:
select * from book where author=’cancer’
"*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,
如:
select * from book where id=1
select * from book where pub_date=#20xx-1-7# (Access)
select * from book where pub_date=’20xx-1-7’ (SQL Server)
提示:
日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法
另外如果是查询传入的变量,则如下:
strau=request.form("author")
strsql="select * from book where author=’"&strau&"’"
如果查询的是数字,则:
intID=request.form("id")
strsql="select * from book where id="&intID
在很多数据库中,如:oracle,上面的语句是可以写成:
strsql="select * from book where id='"&intID&"'"
但是字符型一定不能按照数字格式写,需要注意。
2.添加记录(Insert)
语法:
Insert into table(field1,field2,....) Values (value1,value2,....)
例子:添加一作者是"cancer"的记录入book表:
insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer无组件上传程序’)
同样,如果用到变量就如下:
strno=request.form("bookno")
strau=request.form("author")
strname=request.form("bookname")
strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"
3.用Recordset对象的Addnew插入数据的方法:
语法:
rs.addnew
rs("field1").value="/value1
rs("field2").value="/value2
...
rs.update
4.修改数据记录(Update)
语法:
update"" table set field1=value1,field2=value2,...where fieldx=valuex
例子:
update book set author=’babycrazy’ where bookno=’CF001’
如果用到变量就如下:
strno=request.form("bookno")
strau=request.form("author")
strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"
5.Recordset对象的Update方法:
语法:
rs("field1").value="/value1
rs("field2").value="/value2
...
rs.update
注意:使用语法3和语法5的时候,一定要注意字段的类型(尤其是日期型)一致,否
则出错的几率非常的高。
例子:
strno=request.form("bookno")
strau=request.form("""author")
set adocon=server.createobject("adodb.connection")
adocon.open "Driver={Microsoft Access Driver(*.mdb)};DBQ=" & _
Server.Mappath=("/cancer/cancer.mdb")
strsql="select * from book where bookno=’"&strno&"’"
set rs=server.createobject("adodb.recordset")
rs.open strsql,adconn,1,3
if not rs.eof then ’如果有此记录的话
rs("author").value="/strau
rs.update
end" if
rs.close
set rs=nothing
adocon.close
set adocon=nothing
6.删除一条记录(Delete)
语法:
Delete table where field=value
例子:删除book表中作者是cancer的记录
delete book where author=’cancer’
(注意:如果book表中author字段的值为cancer的记录有多条,将会删除所有author为cancer的记录)