欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫(kù)

 更新時(shí)間:2015年11月25日 10:43:08   作者:依舊若塵  
這篇文章主要介紹了PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下

環(huán)境

開發(fā)包:appserv-win32-2.5.10

服務(wù)器:Apache2.2

數(shù)據(jù)庫(kù):phpMyAdmin

語言:php5,java

平臺(tái):windows 10

java驅(qū)動(dòng):mysql-connector-java-5.1.37

需求

編寫一個(gè)PHP腳本語言,連接到phpMyAdmin數(shù)據(jù)庫(kù)的test庫(kù)

編寫一個(gè)java web服務(wù)端,連接到phpMyAdmin數(shù)據(jù)庫(kù)的test庫(kù)

代碼

php連接方式

mysql.php

<?php
/*****************************
*數(shù)據(jù)庫(kù)連接
*****************************/
$conn = @mysql_connect("localhost","root","123");
if (!$conn){
  die("連接數(shù)據(jù)庫(kù)失?。? . mysql_error());
}
mysql_select_db("test", $conn);
//字符轉(zhuǎn)換,讀庫(kù)
mysql_query("set character set utf8");
mysql_query("set names utf8");
?>

test.php測(cè)試

<?php 
  error_reporting(0);     //防止報(bào)錯(cuò)
  include('mysql.php');
  $result=mysql_query("select * from user"); //根據(jù)前面的計(jì)算出開始的記錄和記錄數(shù)
  // 循環(huán)取出記錄
  $six;
  while($row=mysql_fetch_row($result))
  {  
  echo $row[0];
  echo $row[1];
  }
?>

 運(yùn)行截圖 :

java 連接方式

1.新建一個(gè)java project為mysqlTest

2.加載JDBC驅(qū)動(dòng),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ù)庫(kù)連接地址
 * user 數(shù)據(jù)庫(kù)登陸賬號(hào)
 * password 數(shù)據(jù)庫(kù)登陸密碼
 * 方法:
 * 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ū)動(dòng) Class.forName(Driver) 第二步:創(chuàng)建連接
   * DriverManager.getConnection(url, user, password);
   */
  public Connection conn() {
    try {
      Class.forName(driver);
    } catch (ClassNotFoundException e) {
      System.out.println("驅(qū)動(dòng)加載錯(cuò)誤");
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
      System.out.println("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤");
      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ù)庫(kù)中語句

我們常常用conn.php文件來建立與數(shù)據(jù)庫(kù)的鏈接,然后在所需的文件中利用include 進(jìn)行調(diào)用。這樣有效防止對(duì)數(shù)據(jù)庫(kù)屬性的改動(dòng) 而引起其他有關(guān)文件對(duì)數(shù)據(jù)調(diào)用的錯(cuò)誤。

  現(xiàn)在來看一個(gè)conn.php文件,代碼如下:

<?php
 $conn=@mysql_connect("localhost","root","")or die("數(shù)據(jù)庫(kù)連接錯(cuò)誤");//鏈接數(shù)據(jù)庫(kù)服務(wù)器
 mysql_select_db("messageboard",$conn);//選擇數(shù)據(jù)庫(kù)名為messageboard
 mysql_query("set names 'utf'");//使用utf編碼,這里不能寫成utf-否則將顯示亂碼,但UTF不區(qū)分大小寫
 ?>

學(xué)習(xí)積累,收集了PHP操作MYSQL的幾個(gè)基礎(chǔ)函數(shù):

.使用mysql_connect()函數(shù)連接MySQL服務(wù)器:mysql_connect("hostname", "username","password");
如,$link = mysql_connect("localhost", "root", "") or die("不能連接到數(shù)據(jù)庫(kù)服務(wù)器!可能是數(shù)據(jù)庫(kù)服務(wù)器沒有啟動(dòng),或者用戶名密碼有誤!".mysql_error());

.使用mysql_select_db()函數(shù)選擇數(shù)據(jù)庫(kù)文件:mysql_query("use 數(shù)據(jù)庫(kù)名",$link);

如,$db_selected=mysql_query("use example",$link);

.使用mysql_query()函數(shù)執(zhí)行SQL語句:mysql_query(string query(SQL語句),$link);

如:

添加會(huì)員:$result=mysql_query("insert into tb_member values('a','')",$link);

修改會(huì)員:$result=mysql_query("update tb_member setuser='b',pwd=''where user='a'",$link);

刪除會(huì)員:$result=mysql_query("delecte from tb_member where user='b'",$link);

查詢會(huì)員:$sql=mysql_query("select * from tb_book");

模糊查詢:$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");

//通用符%表示零個(gè)或任意多個(gè)字符。

顯示表結(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:可選項(xiàng),php操作MySQL數(shù)據(jù)庫(kù)語句基礎(chǔ)整數(shù)型參數(shù),要傳入的是MYSQL_ASSOC(關(guān)聯(lián)索引)、MYSQL_NUM(數(shù)字索引) MYSQL_BOTH(包括前兩者,默認(rèn)值)

如:

<>$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é)果集中獲取一行作為對(duì)象:

語法結(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ù)類似,只有一點(diǎn)區(qū)別,即返回一個(gè)對(duì)象而不是數(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ù)來實(shí)現(xiàn)。

.mysql_query("set names gb");//設(shè)置MySQL的編碼格式為 gb類型,以屏蔽亂碼。

.關(guān)閉記錄集:mysql_free_result($sql);

.關(guān)閉MySQL數(shù)據(jù)庫(kù)服務(wù)器:mysql_close($conn);

相關(guān)文章

最新評(píng)論