PHP连接数据库之PHP连接MYSQL数据库代码
< ?php $mysql_server_name= localhost //改成自己的mysql数据库服务器 $mysql_username= root //改成自己的mysql数据库用户名 $mysql_password= //改成自己的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( ) UNSIGNED NOT NULL AUTO_INCREMENT `count` INT( ) UNSIGNED NOT NULL DEFAULT PRIMARY KEY ( `id` ) ) TYPE = innodb mysql_select_db($mysql_database $conn) $result=mysql_query($sql) //echo $sql mysql_close($conn) echo "Hello!数据库mycounter已经成功建立!" ?>PHP连接数据库之PHP连接ACCESS数据库代码方法
< ? $conn = new ("ADODB Connection") $connstr = "DRIVER={Microsoft Access Driver (* mdb)} DBQ=" realpath("data/db mdb") $conn >Open($connstr) $rs = new ("ADODB RecordSet") $rs >Open("select * from szd_t" $conn ) while(! $rs >eof) { $f = $rs >Fields( ) echo $f >value $rs >MoveNext() } ?>
PHP连接数据库之PHP连接MS SQL数据库代码方法
安装SQL服务器并添加PHP的MSSQL扩展
使用以下代码连接并测试
< ?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 = WHERECountry=USA AND Left(HomePhone ) = ( ) $result = mssql_query($query) $numRows = mssql_num_rows($result) echo <h > $numRows Row ($numRows == ? : s) Returned </ h > while($row = mssql_fetch_array($result)) { echo <li> $row[Employee] </li> } ?>
PHP连接数据库之PHP连接Oracle数据库
PHP提供了两套函数与Oracle连接 分别是ORA_和OCI函数 其中ORA_函数略显陈旧 OCI函数更新据说更好一些 两者的使用语法几乎相差无几 你的PHP安装选项应该可以支持两者的使用
< ? if ($conn=Ora_Logon("user@TNSNAME" "password")) { echo "SUCCESS ! Connected to databasen" }else {echo "Failed : ( Could not connect to databasen"} Ora_Logoff($conn) phpinfo() ?> lishixinzhi/Article/program/PHP/201405/30761
package mysql
import java.sql.*
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName"
String user = "mysqluser"
String password = "password"
String driverClass = "com.mysql.cj.jdbc.Driver"
Connection connection = null
Class.forName(driverClass)
try {
connection = DriverManager.getConnection(url, user, password)
} catch (SQLException e) {
e.printStackTrace()
}
if (connection != null) {
System.out.println("数据库连接成功")
} else {
System.out.println("数据库连接失败")
connection.close()
}
return connection
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement()
// 要执行的 Mysql 数据库 *** 作语句(增、删、改、查)
String sql = ""
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql)
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id")
String name = resultSet.getString("name")
// 输出数据
System.out.println("ID : " +id)
System.out.println("name :" + name)
}
// 完成后需要依次关闭
resultSet.close()
statement.close()
getConnection().close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)