一、JDBC连接各种数据库
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password);
2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);
4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance(); String url =" jdbc:sybase:Tds:localhost:5007/myDB"; //myDB为你的数据库名 Properties sysProps = System.getProperties(); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //myDB为数据库名 Connection conn= DriverManager.getConnection(url);
6、MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB为数据库名 Connection conn= DriverManager.getConnection(url);
7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名 String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password); (http://www.fanqiang.com)
二、ASP连接各种数据库的方法
ODBC链接
适合数据库类型 链接方式 access "Driver={microsoft access driver(*.mdb)};dbq=*.mdb;uid=admin;pwd=pass;" dBase "Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;" Oracle "Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;" MSSQL server "Driver={sql server};server=servername;database=dbname;uid=sa;pwd=pass;" MS text "Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;extensions=asc,csv,tab,txt;Persist SecurityInfo=false;" Visual Foxpro "Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;" MySQL "Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=;"
OLEDB链接
适合的数据库类型 链接方式 access "Provider=microsoft.jet.oledb.4.0;data source=your_database_path;user id=admin;password=pass;" Oracle "Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;" MS SQL Server "Provider=SQLOLEDB;data source=machinename;initial catalog=dbname;userid=sa;password=pass;" MS text "Provider=microsof.jet.oledb.4.0;data source=your_path;Extended Properties′text;FMT=Delimited′"
三、PDO对各种数据库的连接方法
PgSQL
php try { $db = new PDO("pgsql:dbname=pdo;host=localhost", "username", "password" ); echo "PDO connection object created"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
SQLite
php try { /*** connect to SQLite database ***/ $dbh = new PDO("sqlite:/path/to/database.sdb"); } catch(PDOException $e) { echo $e->getMessage(); } ?>
PDO还能在内存中创建SQLite并使用
php try { /*** connect to SQLite database ***/ $db = new PDO("sqlite::memory"); /*** a little message to say we did it ***/ echo 'database created in memory'; } catch(PDOException $e) { echo $e->getMessage(); } ?>
MySQL
php /*** mysql hostname ***/ $hostname = 'localhost'; /*** mysql username ***/ $username = 'username'; /*** mysql password ***/ $password = 'password'; try { $dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password); /*** echo a message saying we have connected ***/ echo 'Connected to database'; } catch(PDOException $e) { echo $e->getMessage(); } ?>
Firebird
php try { $dbh = new PDO("firebird:dbname=localhost:C:/Programs/Firebird/DATABASE.FDB", "SYSDBA", "masterkey"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
Informix
php try { $dbh = new PDO("informix:DSN=InformixDB", "username", "password"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
Oracle
简单连接
php try { $dbh = new PDO("OCI:", "username", "password") } catch (PDOException $e) { echo $e->getMessage(); } ?>
带数据库名和字符集连接
php try { $dbh = new PDO("OCI:dbname=accounts;charset=UTF-8", "username", "password"); } catch (PDOException $e) { echo $e->getMessage(); } ?> ODBC php try { $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:/accounts.mdb;Uid=Admin"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
DBLIB
php try { $hostname = "localhost"; $port = 10060; $dbname = "my_database"; $username = "username"; $password = "password"; $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$password"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
IBM
php try { $db = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=accounts; HOSTNAME=1.2.3,4;PORT=56789;PROTOCOL=TCPIP;", "username", "password"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
四、PHP连接各种数据库方法代码
PHP连接MYSQL数据库代码
________________________________________ php $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mysql_username='root'; //改成自己的mysql数据库用户名 $mysql_password='12345678'; //改成自己的mysql数据库密码 $mysql_database='mycounter'; //改成自己的mysql数据库名 $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); $sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; '; mysql_query($sql); $sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;'; mysql_select_db($mysql_database,$conn); $result=mysql_query($sql); //echo $sql; mysql_close($conn); echo "Hello!数据库mycounter已经成功建立!"; ?>
PHP连接ACCESS数据库代码方法
________________________________________ $conn = new com("ADODB.Connection"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("data/db.mdb"); $conn->Open($connstr); $rs = new com("ADODB.RecordSet"); $rs->Open("select * from szd_t",$conn,1,1); while(! $rs->eof) { $f = $rs->Fields(1); echo $f->value; $rs->MoveNext(); } ?>
PHP连接MS SQL数据库代码方法
________________________________________ 1.安装SQL服务器并添加PHP的MSSQL扩展 2.使用以下代码连接并测试 php $myServer = localhost; //主机 $myUser = sa; //用户名 $myPass = password; //密码 $myDB = Northwind; //MSSQL库名 $s = @mssql_connect($myServer, $myUser, $myPass) or die(Couldnt connect to SQL Server on $myServer); $d = @mssql_select_db($myDB, $s) or die(Couldnt open database $myDB); $query = SELECT TitleOfCourtesy+ +FirstName+ +LastName AS Employee ; $query .= FROM Employees ; $query .= WHERE Country=USA AND Left(HomePhone, 5) = (206); $result = mssql_query($query); $numRows = mssql_num_rows($result); echo
PHP连接Oracle数据库
PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函数更新据说更好一些。两者的使用语法几乎相差无几。你的PHP安装选项应该可以支持两者的使用。 连接 < ? if ($conn=Ora_Logon("user@TNSNAME","password")) { echo "SUCCESS ! Connected to database/n"; }else {echo "Failed :-( Could not connect to database/n";} Ora_Logoff($conn); phpinfo(); ?> 以上代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。
五、jsp连接数据库的各种方法
在练习这些代码的时候,一定将jdbc的驱动程序放到服务器的类路径里,然后要 在数据库里建一个表test,有两个字段比如为test1,test2,可以用下面SQL建: create table test(test1 varchar(20),test2 varchar(20)
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为你的数据库的SID String user="scott"; String password="tiger"; Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
二、jsp连接Sql Server数据库
testsqlserver.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver: //localhost:1433;DatabaseName=pubs"; //pubs为你的数据库的 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
三、jsp连接DB2数据库
testdb2.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2: //localhost:5000/sample"; //sample为你的数据库名 String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <;%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
四、jsp连接Informix数据库
testinformix.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli: //123.45.67.89:1533/testDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //testDB为你的数据库名 Connection conn= DriverManager.getConnection(url); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
五、jsp连接Sybase数据库
testmysql.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("com.sybase.jdbc.SybDriver").newInstance(); String url =" jdbc:sybase:Tds:localhost:5007/tsdata"; //tsdata为你的数据库名 Properties sysProps = System.getProperties(); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
六、jsp连接MySQL数据库
testmysql.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //testDB为你的数据库名 Connection conn= DriverManager.getConnection(url); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>
七、jsp连接PostgreSQL数据库
testmysql.jsp如下: <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <%Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/soft" //soft为你的数据库名 String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from test"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %>