PHP學習筆記——PHP指令碼和JAVA串連mysql資料庫

來源:互聯網
上載者:User

標籤:

環境

開發包: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

<?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 project為mysqlTest

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 擷取串連 */public class MySQLConnection {    public static Connection conn = null;    public static String driver = "com.mysql.jdbc.Driver";    public static String url = "jdbc:mysql://127.0.0.1:3306/post";    public static String user = "root";    public static String password = "123";    /*     * 建立Mysql資料連線 第一步:載入驅動 Class.forName(Driver) 第二步:建立串連     * 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增刪改查 */public class Work {    /*     * insert 增加     */    public static int 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 寫入     */    public static void 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 修改     */    public static int 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 刪除     */    public static int 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     */    public static void main(String[] args) {        // System.out.println(insert());         select();        // System.out.println(update());        // System.out.println(delete());    }}

 test

PHP學習筆記——PHP指令碼和JAVA串連mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.