This article mainly introduces the PHP script and related information about connecting to the mysql database by using JAVA. For more information, see
This article mainly introduces the PHP script and related information about connecting to the mysql database by using JAVA. For more information, see
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
Requirement
Write a PHP script to connect to the test database of the phpMyAdmin database.
Write a java web server that connects to the test database of the phpMyAdmin Database
Code
Php connection method
Mysql. php
<? Php/****************************** database connection ****** * ********************/$ conn = @ mysql_connect ("localhost ", "root", "123"); if (! $ Conn) {die ("failed to connect to the database :". mysql_error ();} mysql_select_db ("test", $ conn); // character conversion, read database mysql_query ("set character set utf8 "); mysql_query ("set names utf8");?>
Test. php test
<? Phperror_reporting (0); // prevents include ('mysql. php '); $ result = mysql_query ("select * from user"); // calculate the initial record and number of records based on the previous calculation. // retrieve the record in a loop $ six; while ($ row = mysql_fetch_row ($ result) {echo $ row [0]; echo $ row [1] ;}?>
Run:
Java connection
1. Create a New java project named mysqlTest
2. Load JDBC driver, mysql-connector-java-5.1.37
MySQLConnection. java
Package com. mysqltest; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException;/***** Mysql connection ***** parameter: * conn connection * url mysql database connection address * user database login account * password Database login password * method: * conn get Connection */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 "; /** create Mysql Data Connection Step 1: load the driver Class. forName (Driver) Step 2: create a connection * DriverManager. getConnection (url, user, password); */public Connection 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 () ;}return conn ;}}
Work. java
Package com. mysqltest; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException;/** add, delete, modify, and query mysql */public class Work {/** insert add */public static int insert () {MySQLConnection connection Connection = new MySQLConnection (); connection conns; // obtain the connection PreparedStatement pst; // execute the SQL statement 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.exe cuteUpdate (); pst. close (); conns. close ();} catch (SQLException e) {System. out. println ("Data Writing failed"); e. printStackTrace ();} return I;}/** select write */public static void select () {MySQLConnection connection = new MySQLConnection (); Connection conns; // obtain the connection PreparedStatement pst ;// Execute the SQL Statement (Statement) ResultSet rs; // obtain the returned result String SQL = "select * from user"; try {conns = connection. conn (); pst = conns. prepareStatement (SQL); rs = pst.exe cuteQuery (SQL); // execute the SQL statement System. 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 modify */public static int update () {MySQLConnection connection Connection = new MySQLConnection (); connection conns; // obtain the Connection PreparedStatement pst; // execute the SQL Statement (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.exe cuteUpdate (); pst. close (); conns. close ();} catch (SQLException e) {System. out. println ("data modification failed"); e. printStackTrace ();} return I;}/** delete */public static int delete () {MySQLConnection connection = new MySQLConnection (); Connection conns; // get the connection PreparedStatement pst; // Execute Row SQL Statement (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.exe cuteUpdate (); pst. close (); conns. close ();} catch (SQLException e) {System. out. println ("data deletion failed"); 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 statements used to operate MySQL Databases
We often use the conn. php file to establish a connection with the database, and then use include to call the file. This effectively prevents data calling errors caused by changes to database attributes.
Now let's take a look at the conn. php file. The Code is as follows: