如如何将网页连接到数据库?

如如何将网页连接到数据库?,第1张

一、要实现用户输入,肯定前台要是一个网页,使用jsp,先建一个jsp网页。

二、使用servlet,在jsp文件中,需要一个表单form把数据传到servlet中,antion里面写servlet的地址,method里面写方法“get或者post”,一般删除数据默认是“get”添加数据是“post”。

三、在servlet中来获取页面上的值,注意获取的值是String类型的。 如果要用到int型,就想要强转类型。

四、把数据插入到 对象的属性中,使用set的方法。

五、调用add的方法,就可以把数据插入到后台。

六、具体的插入到数据库的代码要注意数据库的链接。

1、首先在电脑端打开Dreamwerver软件,如下图所示。

2、然后打开站点文件,新建一个php的站点文件,在文件夹中建立一个news.php的文件,然后双击打开。

3、然后在Dreamwerver软件中选择应用程序选下的数据库,点击加号,对数据库进行连接。

4、如果软件中没有应用程序这个选项,在上面的那个窗口位置,点击打开,里面有数据库选项,打开即可。

5、打开应用程序以后,输入需要连接的数据库的信息,接着点击数据库选项按钮。

6、选择好数据库点击确定即可,系统会自动创建一个文件,就是数据库链接的文件了,如下图所示。

常规方式

常规方式就是按部就班的读取文件了。其余的话和上述方案一致。

// 读取配置文件内容

$handle = fopen("filepath", "r")           $content = fread($handle, filesize("filepath"))123

PHP解析XML

上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。

配置文件

<?xml version="1.0" encoding="UTF-8" ?><mysql>

<!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 -->

<host>localhost</host>

<user>root</user>

<password>123456</password>

<db>test</db>

<port>3306</port></mysql>12345678910

解析

<?php/**

* 作为解析XML配置文件必备工具

*/class XMLUtil {

public static $dbconfigpath = "./db.config.xml"   public static function getDBConfiguration() {

$dbconfig = array ()       try {            // 读取配置文件内容

$handle = fopen(self::$dbconfigpath, "r")           $content = fread($handle, filesize(self::$dbconfigpath))           // 获取xml文档根节点,进而获取相关的数据库信息

$mysql = simplexml_load_string($content)           // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用

$dbconfig['host'] = $mysql->host           $dbconfig['user'] = $mysql->user           $dbconfig['password'] = $mysql->password           $dbconfig['db'] = $mysql->db           $dbconfig['port'] = $mysql->port           // 将配置信息以关联数组的形式返回

return $dbconfig

} catch ( Exception $e ) {            throw new RuntimeException ( "<mark>读取数据库配置文件信息出错!</mark><br />" )

}        return $dbconfig

}

}1234567891011121314151617181920212223242526272829

数据库连接池

对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。

于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。

从池子中取,用毕,归还给池子。

<?php/**x

*  PHP中的数据库 工具类设计

*  郭璞

*  2016年12月23日

*

**/class DbHelper {    private $dbconfig   private $dbpool   public $poolsize   public function __construct($poolsize = 20) {        if (! file_exists ( "./utils.php" )) {            throw new RuntimeException ( "<mark>utils.php文件丢失,无法进行配置文件的初始化 *** 作!</mark><br />" )

}else {

require './utils.php'

}        // 初始化 配置文件信息

$this->dbconfig = XMLUtil::getDBConfiguration ()       // 准备好数据库连接池“伪队列”

$this->poolsize = $poolsize

$this->dbpool = array ()       for($index = 1$index <= $this->poolsize$index ++) {

$conn = mysqli_connect ( $this->dbconfig ['host'], $this->dbconfig ['user'], $this->dbconfig ['password'], $this->dbconfig ['db'] ) or die ( "<mark>连接数据库失败!</mark><br />" )

array_push ( $this->dbpool, $conn )

}

}    /**

* 从数据库连接池中获取一个数据库链接资源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() {        if (count ( $this->dbpool ) <= 0) {            throw new ErrorException ( "<mark>数据库连接池中已无链接资源,请稍后重试!</mark>" )

} else {            return array_pop ( $this->dbpool )

}

}    /**

* 将用完的数据库链接资源放回到数据库连接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) {        if (count ( $this->dbpool ) >= $this->poolsize) {            throw new ErrorException ( "<mark>数据库连接池已满</mark><br />" )

} else {

array_push ( $this->dbpool, $conn )

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10037279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存