首頁 > php教程 > php手册 > PHP学习笔记PHP脚本和JAVA连接mysql数据库,学习笔记mysql

PHP学习笔记PHP脚本和JAVA连接mysql数据库,学习笔记mysql

WBOY
發布: 2016-06-13 08:50:25
原創
727 人瀏覽過

PHP学习笔记——PHP脚本和JAVA连接mysql数据库,学习笔记mysql

环境

开发包:appserv-win32-2.5.10

服务器:Apache2.2

数据库:phpMyAdmin

语言:php5,java

平台:windows 10

java驱动:mysql-connector-java-5.1.37

需求

编写一个PHP脚本语言,连接到phpMyAdmin数据库的test库

编写一个java web服务端,连接到phpMyAdmin数据库的test库

代码

php连接方式

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测试

<?<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>?>
登入後複製

运行截图 :java 连接方式

1.新建一个java project为mysqlTest

2.加载JDBC驱动,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截图

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板