PHP Learning notes--php script and Java connection MySQL database

Source: Internet
Author: User
Tags php script java web

Environment

Development Kit: appserv-win32-2.5.10

Server: Apache2.2

Database: PhpMyAdmin

Language: Php5,java

Platform: Windows 10

Java Driver: mysql-connector-java-5.1.37

Demand

Write a PHP scripting language that connects to the test library of the phpMyAdmin database

Write a Java Web server that connects to the test library of the phpMyAdmin database

Code

PHP connection mode

mysql.php

<?PHP/****************************** Database Connection *****************************/$conn= @mysql_connect("localhost", "root", "123");if(!$conn){     die("Connection Database failed:".)Mysql_error());}mysql_select_db("Test",$conn);//character conversion, reading librarymysql_query("Set character set UTF8");mysql_query("Set Names UTF8");?>

test.php Test

<?PHPerror_reporting(0);//Prevent Errors    include(' mysql.php '); $result=mysql_query("SELECT * from user");//based on the previous calculation of the starting record and number of records//loop fetch record    $six;  while($row=Mysql_fetch_row($result))    {        Echo $row[0]; Echo $row[1]; }?>

Run:

Java Connection mode

1. Create a new Java project for Mysqltest

2. Load the JDBC driver, mysql-connector-java-5.1.37

Mysqlconnection.java

 Packagecom.mysqltest;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;/** **MYSQL Connection * * * parameter: * conn connection * URL MySQL database connection address * User Database login account * Password Database login Password * Method: * Conn Get the connection*/ Public classmysqlconnection { Public StaticConnection conn =NULL;  Public StaticString Driver = "Com.mysql.jdbc.Driver";  Public StaticString url = "Jdbc:mysql://127.0.0.1:3306/post";  Public StaticString user = "root";  Public StaticString Password = "123"; /** Create MySQL data connection first step: Load driver Class.forName (Driver) Second step: Create connection * drivermanager.getconnection (URL, user, password); */     PublicConnection Conn () {Try{class.forname (driver); } Catch(ClassNotFoundException e) {System.out.println ("Driver loading Error");        E.printstacktrace (); }        Try{conn=drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {System.out.println ("Database link Error");        E.printstacktrace (); }        returnConn; }}

Work.java

 Packagecom.mysqltest;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;/** MySQL additions and deletions change*/ Public classWork {/** Insert Add*/     Public Static intInsert () {mysqlconnection connection=Newmysqlconnection (); Connection Conns; //Get ConnectionsPreparedStatement PST;//Execute SQL statement        inti = 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 ("Data Write Failed");        E.printstacktrace (); }        returni; }    /** Select Write*/     Public Static voidSelect () {mysqlconnection connection=Newmysqlconnection (); Connection Conns; //Get ConnectionsPreparedStatement PST;//Execute SQL statement (Statement)ResultSet rs;//Get return resultsString sql = "SELECT * from User"; Try{Conns=Connection.conn (); PST=conns.preparestatement (SQL); RS= Pst.executequery (SQL);//Execute SQL statementSystem.out.println ("---------------------------------------"); System.out.println ("Name | Password);  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 ("Data query Failed");        E.printstacktrace (); }    }    /** Update Changes*/     Public Static intUpdate () {mysqlconnection connection=Newmysqlconnection (); Connection Conns; //Get ConnectionsPreparedStatement PST;//Execute SQL statement (Statement)        inti = 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 ("Data modification Failed");        E.printstacktrace (); }        returni; }    /** Delete deletion*/     Public Static intDelete () {mysqlconnection connection=Newmysqlconnection (); Connection Conns; //Get ConnectionsPreparedStatement PST;//Execute SQL statement (Statement)        inti = 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 ("Data deletion failed");        E.printstacktrace (); }        returni; }    /** Test*/     Public Static voidMain (string[] args) {//System.out.println (Insert ());Select (); //System.out.println (update ()); //System.out.println (delete ());    }}

Test

PHP Learning notes--php script and Java connection MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.