Home >Backend Development >PHP Tutorial >PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql_PHP tutorial

PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:04:111015browse

PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql

Environment

Development package: appserv-win32-2.5.10

Server: Apache2.2

Database: phpMyAdmin

Language: php5, java

Platform: windows 10

java driver: mysql-connector-java-5.1.37

Demand

Write a PHP script language and connect to the test library of phpMyAdmin database

Write a java web server and connect to the test library of phpMyAdmin database

Code

php connection method

mysql.php

<?<span>php
</span><span>/*</span><span>****************************
*数据库连接
****************************</span><span>*/</span>
<span>$conn</span> = @<span>mysql_connect</span>("localhost","root","123"<span>);
</span><span>if</span> (!<span>$conn</span><span>){
    </span><span>die</span>("连接数据库失败:" . <span>mysql_error</span><span>());
}

</span><span>mysql_select_db</span>("test", <span>$conn</span><span>);
</span><span>//</span><span>字符转换,读库</span>
<span>mysql_query</span>("set character set utf8"<span>);
</span><span>mysql_query</span>("set names utf8"<span>);

</span>?>

test.php test

<?<span>php 
    </span><span>error_reporting</span>(0);         <span>//</span><span>防止报错</span>
    <span>include</span>('mysql.php'<span>);
    </span><span>$result</span>=<span>mysql_query</span>("select * from user"); <span>//</span><span>根据前面的计算出开始的记录和记录数
    // 循环取出记录</span>
    <span>$six</span><span>;
    </span><span>while</span>(<span>$row</span>=<span>mysql_fetch_row</span>(<span>$result</span><span>))
    {    
    </span><span>echo</span> <span>$row</span>[0<span>];
    </span><span>echo</span> <span>$row</span>[1<span>];
    }
</span>?>

Running screenshot: PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql_PHP tutorialjava connection method

1. Create a new java project as mysqlTest

2. Load JDBC driver, mysql-connector-java-5.1.37

MySQLConnection.java

<span>package</span><span> com.mysqltest;

</span><span>import</span><span> java.sql.Connection;
</span><span>import</span><span> java.sql.DriverManager;
</span><span>import</span><span> java.sql.SQLException;

</span><span>/*</span><span>
 * **Mysql连接**
 * 
 * 参数:
 * conn 连接
 * url mysql数据库连接地址
 * user 数据库登陆账号
 * password 数据库登陆密码
 * 方法:
 * conn 获取连接
 </span><span>*/</span>
<span>public</span> <span>class</span><span> MySQLConnection {

    </span><span>public</span> <span>static</span> Connection conn = <span>null</span><span>;
    </span><span>public</span> <span>static</span> String driver = "com.mysql.jdbc.Driver"<span>;
    </span><span>public</span> <span>static</span> String url = "jdbc:mysql://127.0.0.1:3306/post"<span>;
    </span><span>public</span> <span>static</span> String user = "root"<span>;
    </span><span>public</span> <span>static</span> String password = "123"<span>;

    </span><span>/*</span><span>
     * 创建Mysql数据连接 第一步:加载驱动 Class.forName(Driver) 第二步:创建连接
     * DriverManager.getConnection(url, user, password);
     </span><span>*/</span>
    <span>public</span><span> Connection conn() {
        </span><span>try</span><span> {
            Class.forName(driver);
        } </span><span>catch</span><span> (ClassNotFoundException e) {
            System.out.println(</span>"驱动加载错误"<span>);
            e.printStackTrace();
        }
        </span><span>try</span><span> {
            conn </span>=<span> DriverManager.getConnection(url, user, password);
        } </span><span>catch</span><span> (SQLException e) {
            System.out.println(</span>"数据库链接错误"<span>);
            e.printStackTrace();
        }
        </span><span>return</span><span> conn;
    }
}</span>

Work.java

<span>package</span><span> com.mysqltest;

</span><span>import</span><span> java.sql.Connection;
</span><span>import</span><span> java.sql.PreparedStatement;
</span><span>import</span><span> java.sql.ResultSet;
</span><span>import</span><span> java.sql.SQLException;

</span><span>/*</span><span>
 * mysql增删改查
 </span><span>*/</span>
<span>public</span> <span>class</span><span> Work {

    </span><span>/*</span><span>
     * insert 增加
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>int</span><span> insert() {
        MySQLConnection connection </span>= <span>new</span><span> MySQLConnection();
        Connection conns; </span><span>//</span><span> 获取连接</span>
        PreparedStatement pst; <span>//</span><span> 执行Sql语句</span>
        <span>int</span> i = 0<span>;
        String sql </span>= "insert into user (username,password) values(?,?)"<span>;
        </span><span>try</span><span> {
            conns </span>=<span> connection.conn();
            pst </span>=<span> conns.prepareStatement(sql);
            pst.setString(</span>1, "lizi"<span>);
            pst.setString(</span>2, "123"<span>);
            i </span>=<span> pst.executeUpdate();
            pst.close();
            conns.close();
        } </span><span>catch</span><span> (SQLException e) {
            System.out.println(</span>"数据写入失败"<span>);
            e.printStackTrace();
        }
        </span><span>return</span><span> i;
    }

    </span><span>/*</span><span>
     * select 写入
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>void</span><span> select() {
        MySQLConnection connection </span>= <span>new</span><span> MySQLConnection();
        Connection conns; </span><span>//</span><span> 获取连接</span>
        PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span>
        ResultSet rs; <span>//</span><span> 获取返回结果</span>
        String sql = "select * from user"<span>;
        </span><span>try</span><span> {
            conns </span>=<span> connection.conn();
            pst </span>=<span> conns.prepareStatement(sql);
            rs </span>= pst.executeQuery(sql);<span>//</span><span> 执行sql语句</span>
            System.out.println("---------------------------------------"<span>);
            System.out.println(</span>"名字        |        密码"<span>);
            </span><span>while</span><span> (rs.next()) {
                System.out.println(rs.getString(</span>"username") + "        |        " + rs.getString("password"<span>));
            }
            System.out.println(</span>"---------------------------------------"<span>);
            conns.close();
            pst.close();
            rs.close();
        } </span><span>catch</span><span> (SQLException e) {
            System.out.println(</span>"数据查询失败"<span>);
            e.printStackTrace();
        }
    }

    </span><span>/*</span><span>
     * update 修改
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>int</span><span> update() {
        MySQLConnection connection </span>= <span>new</span><span> MySQLConnection();
        Connection conns; </span><span>//</span><span> 获取连接</span>
        PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span>
        <span>int</span> i = 0<span>;
        String sql </span>= "update user set password = ? where username = ?"<span>;
        </span><span>try</span><span> {
            conns </span>=<span> connection.conn();
            pst </span>=<span> conns.prepareStatement(sql);
            pst.setString(</span>1, "123"<span>);
            pst.setString(</span>2, "lizi"<span>);
            i </span>=<span> pst.executeUpdate();
            pst.close();
            conns.close();
        } </span><span>catch</span><span> (SQLException e) {
            System.out.println(</span>"数据修改失败"<span>);
            e.printStackTrace();
        }
        </span><span>return</span><span> i;

    }

    </span><span>/*</span><span>
     * delete 删除
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>int</span><span> delete() {
        MySQLConnection connection </span>= <span>new</span><span> MySQLConnection();
        Connection conns; </span><span>//</span><span> 获取连接</span>
        PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span>
        <span>int</span> i = 0<span>;
        String sql </span>= "delete from user where username = ?"<span>;
        </span><span>try</span><span> {
            conns </span>=<span> connection.conn();
            pst </span>=<span> conns.prepareStatement(sql);
            pst.setString(</span>1, "lizi"<span>);
            i </span>=<span> pst.executeUpdate();
            pst.close();
            conns.close();
        } </span><span>catch</span><span> (SQLException e) {
            System.out.println(</span>"数据删除失败"<span>);
            e.printStackTrace();
        }
        </span><span>return</span><span> i;
    }

    </span><span>/*</span><span>
     * test
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>void</span><span> main(String[] args) {
        </span><span>//</span><span> System.out.println(insert());</span>
<span>         select();
        </span><span>//</span><span> System.out.println(update());
        </span><span>//</span><span> System.out.println(delete());</span>
<span>    }
}<br /><br /></span>

test screenshot

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1074251.htmlTechArticlePHP study notes - PHP script and JAVA connect to mysql database, study notes mysql environment development package: appserv-win32- 2.5.10 Server: Apache2.2 Database: phpMyAdmin Language: php5, j...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn