PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫
環(huán)境
開發(fā)包:appserv-win32-2.5.10
服務(wù)器:Apache2.2
數(shù)據(jù)庫:phpMyAdmin
語言:php5,java
平臺:windows 10
java驅(qū)動:mysql-connector-java-5.1.37
需求
編寫一個PHP腳本語言,連接到phpMyAdmin數(shù)據(jù)庫的test庫
編寫一個java web服務(wù)端,連接到phpMyAdmin數(shù)據(jù)庫的test庫
代碼
php連接方式
mysql.php
<?php
/*****************************
*數(shù)據(jù)庫連接
*****************************/
$conn = @mysql_connect("localhost","root","123");
if (!$conn){
die("連接數(shù)據(jù)庫失?。? . mysql_error());
}
mysql_select_db("test", $conn);
//字符轉(zhuǎn)換,讀庫
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"); //根據(jù)前面的計算出開始的記錄和記錄數(shù)
// 循環(huán)取出記錄
$six;
while($row=mysql_fetch_row($result))
{
echo $row[0];
echo $row[1];
}
?>
運行截圖 :


java 連接方式
1.新建一個java project為mysqlTest
2.加載JDBC驅(qū)動,mysql-connector-java-5.1.37

MySQLConnection.java
package com.mysqltest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
* **Mysql連接**
*
* 參數(shù):
* conn 連接
* url mysql數(shù)據(jù)庫連接地址
* user 數(shù)據(jù)庫登陸賬號
* password 數(shù)據(jù)庫登陸密碼
* 方法:
* 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";
/*
* 創(chuàng)建Mysql數(shù)據(jù)連接 第一步:加載驅(qū)動 Class.forName(Driver) 第二步:創(chuàng)建連接
* DriverManager.getConnection(url, user, password);
*/
public Connection conn() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("驅(qū)動加載錯誤");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
System.out.println("數(shù)據(jù)庫鏈接錯誤");
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; // 執(zhí)行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("數(shù)據(jù)寫入失敗");
e.printStackTrace();
}
return i;
}
/*
* select 寫入
*/
public static void select() {
MySQLConnection connection = new MySQLConnection();
Connection conns; // 獲取連接
PreparedStatement pst; // 執(zhí)行Sql語句(Statement)
ResultSet rs; // 獲取返回結(jié)果
String sql = "select * from user";
try {
conns = connection.conn();
pst = conns.prepareStatement(sql);
rs = pst.executeQuery(sql);// 執(zhí)行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("數(shù)據(jù)查詢失敗");
e.printStackTrace();
}
}
/*
* update 修改
*/
public static int update() {
MySQLConnection connection = new MySQLConnection();
Connection conns; // 獲取連接
PreparedStatement pst; // 執(zhí)行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("數(shù)據(jù)修改失敗");
e.printStackTrace();
}
return i;
}
/*
* delete 刪除
*/
public static int delete() {
MySQLConnection connection = new MySQLConnection();
Connection conns; // 獲取連接
PreparedStatement pst; // 執(zhí)行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("數(shù)據(jù)刪除失敗");
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數(shù)據(jù)庫中語句
我們常常用conn.php文件來建立與數(shù)據(jù)庫的鏈接,然后在所需的文件中利用include 進行調(diào)用。這樣有效防止對數(shù)據(jù)庫屬性的改動 而引起其他有關(guān)文件對數(shù)據(jù)調(diào)用的錯誤。
現(xiàn)在來看一個conn.php文件,代碼如下:
<?php
$conn=@mysql_connect("localhost","root","")or die("數(shù)據(jù)庫連接錯誤");//鏈接數(shù)據(jù)庫服務(wù)器
mysql_select_db("messageboard",$conn);//選擇數(shù)據(jù)庫名為messageboard
mysql_query("set names 'utf'");//使用utf編碼,這里不能寫成utf-否則將顯示亂碼,但UTF不區(qū)分大小寫
?>
學(xué)習(xí)積累,收集了PHP操作MYSQL的幾個基礎(chǔ)函數(shù):
.使用mysql_connect()函數(shù)連接MySQL服務(wù)器:mysql_connect("hostname", "username","password");
如,$link = mysql_connect("localhost", "root", "") or die("不能連接到數(shù)據(jù)庫服務(wù)器!可能是數(shù)據(jù)庫服務(wù)器沒有啟動,或者用戶名密碼有誤!".mysql_error());
.使用mysql_select_db()函數(shù)選擇數(shù)據(jù)庫文件:mysql_query("use 數(shù)據(jù)庫名",$link);
如,$db_selected=mysql_query("use example",$link);
.使用mysql_query()函數(shù)執(zhí)行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)."%'");
//通用符%表示零個或任意多個字符。
顯示表結(jié)構(gòu):$result=mysql_query("DESC tb_member");
.使用mysql_fetch_array()函數(shù)從數(shù)組結(jié)果集中獲得信息:
語法結(jié)構(gòu):array mysql_fetch_array(resource result[,int result_type])
參數(shù)result資源類型的參數(shù),整形型參數(shù),要傳入的是由mysql_fetch_array()函數(shù)返回的數(shù)據(jù)指針;
參數(shù)result_type:可選項,php操作MySQL數(shù)據(jù)庫語句基礎(chǔ)整數(shù)型參數(shù),要傳入的是MYSQL_ASSOC(關(guān)聯(lián)索引)、MYSQL_NUM(數(shù)字索引) 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()函數(shù)從結(jié)果集中獲取一行作為對象:
語法結(jié)構(gòu):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()函數(shù)與mysql_fetch_array()函數(shù)類似,只有一點區(qū)別,即返回一個對象而不是數(shù)組,該函數(shù)只能通過字段名來訪問數(shù)組。訪問結(jié)果集中行的元素的語法結(jié)構(gòu):$row->col_name(列名)
.使用mysql_fetch_row()函數(shù)逐行獲得結(jié)果集中的每條記錄:
語法結(jié)構(gòu):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()函數(shù)獲取結(jié)果集中地記錄數(shù):
語法結(jié)構(gòu):int mysql_num_rows(resource result)
如:
$sql=mysql_query("select * from tb_book");
......
<?php $nums=mysql_num_rows($sql);echo $nums;?>
注:若要獲得insert、update、delete語句的所影響到的數(shù)據(jù),則必須使用mysql_affected_rows()函數(shù)來實現(xiàn)。
.mysql_query("set names gb");//設(shè)置MySQL的編碼格式為 gb類型,以屏蔽亂碼。
.關(guān)閉記錄集:mysql_free_result($sql);
.關(guān)閉MySQL數(shù)據(jù)庫服務(wù)器:mysql_close($conn);
相關(guān)文章
php+mysql不用遞歸實現(xiàn)的無限級分類實例(非遞歸)
這篇文章主要介紹了php+mysql不用遞歸實現(xiàn)的無限級分類實例,重點在不使用遞歸,需要的朋友可以參考下2014-07-07
phpstudy2018 訪問目錄服務(wù)權(quán)限問題
今天小編在更新phpstudy2018,安裝后訪問路徑出現(xiàn),下面通過本文給大家簡單介紹下,感興趣的朋友跟隨腳本之家小編一起看看吧2018-03-03
ThinkPHP5.1框架頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)頁面模版示例
這篇文章主要介紹了ThinkPHP5.1框架頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)頁面模版,結(jié)合實例形式分析了thinkPHP5.1框架進行頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)模板相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
smarty模板引擎從php中獲取數(shù)據(jù)的方法
這篇文章主要介紹了smarty模板引擎從php中獲取數(shù)據(jù)的方法,涉及smarty變量與php代碼的混編技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01

