PHP隨手筆記整理之PHP指令碼和JAVA串連mysql資料庫,javamysql
環境
開發包: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
ps:php操作MySQL資料庫中語句
我們常常用conn.php檔案來建立與資料庫的連結,然後在所需的檔案中利用include 進行調用。這樣有效防止對資料庫屬性的改動 而引起其他有關檔案對資料調用的錯誤。
現在來看一個conn.php檔案,代碼如下:
<?php $conn=@mysql_connect("localhost","root","")or die("資料庫連接錯誤");//連結資料庫伺服器 mysql_select_db("messageboard",$conn);//選擇資料庫名為messageboard mysql_query("set names 'utf'");//使用utf編碼,這裡不能寫成utf-否則將顯示亂碼,但UTF不區分大小寫 ?>
學習積累,收集了PHP操作MYSQL的幾個基礎函數:
.使用mysql_connect()函數串連MySQL伺服器:mysql_connect("hostname", "username","password");
如,$link = mysql_connect("localhost", "root", "") or die("不能串連到資料庫伺服器!可能是資料庫伺服器沒有啟動,或者使用者名稱密碼有誤!".mysql_error());
.使用mysql_select_db()函數選擇資料庫檔案:mysql_query("use 資料庫名",$link);
如,$db_selected=mysql_query("use example",$link);
.使用mysql_query()函數執行SQL語句:mysql_query(string query(SQL語句),$link);
如:
添加會員:$result=mysql_query("insert into tb_member values('a','')",$link);
修改會員:$result=mysql_query("update tb_member setuser='b',pwd=''where user='a'",$link);
刪除會員:$result=mysql_query("delecte from tb_member where user='b'",$link);
查詢會員:$sql=mysql_query("select * from tb_book");
模糊查詢:$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");
//通用符%表示零個或任意多個字元。
顯示表結構:$result=mysql_query("DESC tb_member");
.使用mysql_fetch_array()函數從數組結果集中獲得資訊:
文法結構:array mysql_fetch_array(resource result[,int result_type])
參數result資源類型的參數,整形型參數,要傳入的是由mysql_fetch_array()函數返回的資料指標;
參數result_type:可選項,php操作MySQL資料庫語句基礎整數型參數,要傳入的是MYSQL_ASSOC(關聯索引)、MYSQL_NUM(數字索引) MYSQL_BOTH(包括前兩者,預設值)
如:
<>$sql=mysql_query("select * from tb_book");$info=mysql_fetch_object($sql);<>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");$info=mysql_fetch_object($sql);
.使用mysql_fetch_object()函數從結果集中擷取一行作為對象:
文法結構:object mysql_fetch_object(resource result);
如:
<>$sql=mysql_query("select * from tb_book");$info=mysql_fetch_object($sql);<>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");$info=mysql_fetch_object($sql);
mysql_fetch_object()函數與mysql_fetch_array()函數類似,只有一點區別,即返回一個對象而不是數組,該函數只能通過欄位名來訪問數組。訪問結果集中行的元素的文法結構:$row->col_name(列名)
.使用mysql_fetch_row()函數逐行獲得結果集中的每條記錄:
文法結構:array mysql_fetch_row(resource result)
如:
<>$sql=mysql_query("select * from tb_book");$row=mysql_fetch_row($sql);<>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");$row=mysql_fetch_row($sql);
.使用mysql_num_rows()函數擷取結果集中地記錄數:
文法結構:int mysql_num_rows(resource result)
如:
$sql=mysql_query("select * from tb_book");......<?php $nums=mysql_num_rows($sql);echo $nums;?>
註:若要獲得insert、update、delete語句的所影響到的資料,則必須使用mysql_affected_rows()函數來實現。
.mysql_query("set names gb");//設定MySQL的編碼格式為 gb類型,以屏蔽亂碼。
.關閉記錄集:mysql_free_result($sql);
.關閉MySQL資料庫伺服器:mysql_close($conn);
http://www.bkjia.com/PHPjc/1075074.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1075074.htmlTechArticlePHP隨手筆記整理之PHP指令碼和JAVA串連mysql資料庫,javamysql 環境 開發包:appserv-win32-2.5.10 伺服器:Apache2.2 資料庫:phpMyAdmin 語言:php5,java 平...