PHP脚本和JAVA连接mysql数据库

PHP――PHP脚本和JAVAmysql数据

包:appserv-win32-2.5.10

器:Apache2.2

数据phpMyAdmin

言:php5java

平台:windows 10

java驱动mysql-connector-java-5.1.37

需求

写一个PHP脚本言,接到phpMyAdmin数据test

写一个java web端,接到phpMyAdmin数据test

php接方式

mysql.php

<?php

/*****************************

*数据库连

*****************************/

$conn = @mysql_connect("localhost","root","123");

if (!$conn){

    die("接数据" . mysql_error());

}

 

mysql_select_db("test", $conn);

//字符转换读库

mysql_query("set character set utf8");

mysql_query("set names utf8");

 

?>

test.php测试

<?php

    error_reporting(0);         //防止报错

    include('mysql.php');

    $result=mysql_query("select * from user"); //根据前面的算出开始的记录记录

    // 取出记录

    $six;

    while($row=mysql_fetch_row($result))

    {   

    echo$row[0];

    echo$row[1];

    }

?>

 

 运行截

java接方式

1.新建一个java projectmysqlTest

2.JDBC驱动,mysql-connector-java-5.1.37

MySQLConnection.java

package com.mysqltest;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

/*

 * **Mysql**

 *

 * 参数:

 * conn

 * url mysql数据库连接地址

 * user 数据陆账

 * password 数据

 * 方法:

 * conn

 */

publicclass MySQLConnection {

 

    publicstatic Connection conn = null;

    publicstatic String driver ="com.mysql.jdbc.Driver";

    publicstatic String url ="jdbc:mysql://127.0.0.1:3306/post";

    publicstatic String user ="root";

    publicstatic String password ="123";

 

    /*

     * Mysql数据第一步:加载驱动 Class.forNameDriver第二步:

     *DriverManager.getConnection(url, user, password);

     */

    public Connection conn() {

        try {

           Class.forName(driver);

        } catch (ClassNotFoundException e) {

           System.out.println("驱动载错误");

           e.printStackTrace();

        }

        try {

           conn = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {

           System.out.println("数据库链错误");

            e.printStackTrace();

        }

        return conn;

    }

}

Work.java

package com.mysqltest;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

/*

 * mysql

 */

publicclass Work {

 

    /*

     * insert增加

     */

    publicstaticint insert() {

       MySQLConnection connection = new MySQLConnection();

       Connection conns; //

       PreparedStatement pst; // Sql

        int i = 0;

       String sql = "insert into user (username,password)values(?,?)";

        try {

           conns = connection.conn();

           pst = conns.prepareStatement(sql);

           pst.setString(1, "lizi");

           pst.setString(2, "123");

            i= pst.executeUpdate();

           pst.close();

           conns.close();

        } catch (SQLException e) {

           System.out.println("数据写入失");

           e.printStackTrace();

        }

        return i;

    }

 

    /*

     * select写入

     */

    publicstaticvoid select() {

       MySQLConnection connection = new MySQLConnection();

       Connection conns; //

       PreparedStatement pst; // Sql(Statement)

       ResultSet rs; // 取返回

       String sql = "select * from user";

        try {

           conns = connection.conn();

           pst = conns.prepareStatement(sql);

           rs = pst.executeQuery(sql);// sql

           System.out.println("---------------------------------------");

           System.out.println("名字        |       ");

            while (rs.next()) {

               System.out.println(rs.getString("username") + "        |       " + rs.getString("password"));

            }

           System.out.println("---------------------------------------");

           conns.close();

           pst.close();

           rs.close();

        } catch (SQLException e) {

           System.out.println("数据查询");

           e.printStackTrace();

        }

    }

 

    /*

     * update修改

     */

    publicstaticint update() {

       MySQLConnection connection = new MySQLConnection();

       Connection conns; //

       PreparedStatement pst; // Sql(Statement)

        int i = 0;

       String sql = "update user set password = ? where username =?";

        try {

           conns = connection.conn();

           pst = conns.prepareStatement(sql);

           pst.setString(1, "123");

           pst.setString(2, "lizi");

            i= pst.executeUpdate();

           pst.close();

            conns.close();

        } catch (SQLException e) {

           System.out.println("数据修改失");

           e.printStackTrace();

        }

        return i;

 

    }

 

    /*

     * delete

     */

    publicstaticint delete() {

       MySQLConnection connection = new MySQLConnection();

       Connection conns; //

       PreparedStatement pst; // Sql(Statement)

        int i = 0;

       String sql = "delete from user where username = ?";

        try {

           conns = connection.conn();

           pst = conns.prepareStatement(sql);

           pst.setString(1, "lizi");

            i= pst.executeUpdate();

           pst.close();

           conns.close();

        } catch (SQLException e) {

           System.out.println("数据除失");

            e.printStackTrace();

        }

        return i;

    }

 

    /*

     * test

     */

    publicstaticvoid main(String[] args) {

        //System.out.println(insert());

        select();

        //System.out.println(update());

        // System.out.println(delete());

    }

}

 


你可能感兴趣的:(PHP)