PHP Learning notes--php script and Java connection MySQL database, learning notes MySQL
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 ) { diemysql_error());} mysql_select_db $conn ); // character conversion, reading library mysql_query ("Set character set UTF8"); mysql_query ("Set names UTF8");? >
test.php Test
PHP error_reporting(0); // Prevent errors include (' mysql.php '); $result=mysql_query/ / 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
http://www.bkjia.com/PHPjc/1074251.html www.bkjia.com true http://www.bkjia.com/PHPjc/1074251.html techarticle PHP Learning Note--php script and Java connection MySQL database, learning note MySQL Environment Development package: APPSERV-WIN32-2.5.10 Server: Apache2.2 database: PhpMyAdmin language: Php5,j ...