import java.sql.*
/**
* @功能: 一个JDBC的本地化API连接类,封装了数据 *** 作方法,只用传一个SQL语句即可
* @作者: 李开欢
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 这里可以将常量全部放入另一个类中,以方便修改
*/
private static Connection conn
private static Statement ps
private static ResultSet rs
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433DatabaseName=mydb"
private static final String USER ="sa"
private static final String PASS = "sa"
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection()
}
public static Connection getConnection(){
System.out.println("连接中...")
try {
Class.forName(ConnectionDemo.DRIVER)
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS)
System.out.println("成功连接")
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return conn
}
public static Statement getStatement(String sql){
System.out.println("执行SQL语句中...")
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql)
System.out.println("执行完查询 *** 作,结果已返回ResultSet集合")
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql)
System.out.println("已执行完毕删除 *** 作")
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql)
System.out.println("已执行完毕增加 *** 作")
}else{
ps.executeUpdate(sql)
System.out.println("已执行完毕更新 *** 作")
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return ps
}
public static ResultSet getResultSet(){
System.out.println("查询结果为:")
return rs
}
public static void closeConnection(){
System.out.println("关闭连接中...")
try {
if (rs != null) {
rs.close()
System.out.println("已关闭ResultSet")
}
if (ps != null) {
ps.close()
System.out.println("已关闭Statement")
}
if (conn != null) {
conn.close()
System.out.println("已关闭Connection")
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection()
String sql = "delete from type where id = 1"
ConnectionDemo.getStatement(sql)
String sql2 = "insert into type values(1,'教学设备')"
ConnectionDemo.getStatement(sql2)
String sql1 = "select * from type"
ConnectionDemo.getStatement(sql1)
ResultSet rs = ConnectionDemo.getResultSet()
System.out.println("编号 "+"类型")
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ")
System.out.println(rs.getString(2))
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
ConnectionDemo.closeConnection()
}
}
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
您好,mysqli有2种编写方法,一种是面向过程,一种是面向对象。
官网上面有面向对象的写法,您可以到php官网参考一下。
mysqli的面向过程基本上和mysql的思路差不多,也是先连接,选择数据库,查询,取结果...
$sql = "SELECT * FROM table"
/* 连接 */
$con = mysqli_connect("localhost","user","password","dbname")
if (!$con)
{
die("连接错误: " . mysqli_connect_error())
}
/* 查询 */
$Result = mysqli_query($con,$sql)
/* 遍历结果集 */
while($data = mysqli_fetch_assoc($Result))
{
$Data_Username = $data["username"]
$Data_Password = $data["password"]
echo "用户名:{$Data_Username},密码:{$Data_Password}"
}
/* 释放查询结果 */
mysqli_free_result($Result)
/* 关闭连接 */
mysqli_close($con)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)