In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how PHP scripts and JAVA connect to mysql databases. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Environment
Development package: 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 to connect 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
Test.php test
Run the screenshot:
Java connection mode
1. Create a new java project as mysqlTest
two。 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 * * parameters: * conn connection * url mysql database connection address * user database login account * password database login password * method: * conn acquires 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 driver Class.forName (Driver) step 2: create 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;/* * mysql addition, deletion, modification and query * / public class Work {/ * * insert add * / public static int insert () {MySQLConnection connection = new MySQLConnection (); Connection conns; / / get connection PreparedStatement pst; / / execute 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.executeUpdate (); pst.close (); conns.close () } catch (SQLException e) {System.out.println ("data write failed"); e.printStackTrace ();} return I;} / * * select write * / public static void select () {MySQLConnection connection = new MySQLConnection (); Connection conns; / / get connection PreparedStatement pst; / / execute Sql statement (Statement) ResultSet rs / / get the returned result String sql = "select * from user"; try {conns = connection.conn (); pst = conns.prepareStatement (sql); rs = pst.executeQuery (sql); / / execute 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 failure"); e.printStackTrace ();}} / * * update modification * / public static int update () {MySQLConnection connection = new MySQLConnection (); Connection conns; / / get connection PreparedStatement pst / / execute 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.executeUpdate (); pst.close (); conns.close ();} catch (SQLException e) {System.out.println ("data modification failure"); e.printStackTrace () } return I;} / * * delete delete * / public static int delete () {MySQLConnection connection = new MySQLConnection (); Connection conns; / / get connection PreparedStatement pst; / / execute 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.executeUpdate (); pst.close (); conns.close ();} catch (SQLException e) {System.out.println ("data deletion failure"); 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 screenshot
Ps:php manipulates statements in MySQL database
We often use conn.php files to establish a link to the database, and then use include to make calls in the files we need. This can effectively prevent changes to database properties that lead to errors in other related files' calls to data.
Now let's look at a conn.php file with the following code:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.