


PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql_PHP tutorial
PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql
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 script language and connect to the test library of phpMyAdmin database
Write a java web server and connect to the test library of phpMyAdmin database
Code
php connection method
mysql.php
<?<span>php </span><span>/*</span><span>**************************** *数据库连接 ****************************</span><span>*/</span> <span>$conn</span> = @<span>mysql_connect</span>("localhost","root","123"<span>); </span><span>if</span> (!<span>$conn</span><span>){ </span><span>die</span>("连接数据库失败:" . <span>mysql_error</span><span>()); } </span><span>mysql_select_db</span>("test", <span>$conn</span><span>); </span><span>//</span><span>字符转换,读库</span> <span>mysql_query</span>("set character set utf8"<span>); </span><span>mysql_query</span>("set names utf8"<span>); </span>?>
test.php test
<?<span>php </span><span>error_reporting</span>(0); <span>//</span><span>防止报错</span> <span>include</span>('mysql.php'<span>); </span><span>$result</span>=<span>mysql_query</span>("select * from user"); <span>//</span><span>根据前面的计算出开始的记录和记录数 // 循环取出记录</span> <span>$six</span><span>; </span><span>while</span>(<span>$row</span>=<span>mysql_fetch_row</span>(<span>$result</span><span>)) { </span><span>echo</span> <span>$row</span>[0<span>]; </span><span>echo</span> <span>$row</span>[1<span>]; } </span>?>
Running screenshot: java connection method
1. Create a new java project as mysqlTest
2. Load JDBC driver, mysql-connector-java-5.1.37
MySQLConnection.java
<span>package</span><span> com.mysqltest; </span><span>import</span><span> java.sql.Connection; </span><span>import</span><span> java.sql.DriverManager; </span><span>import</span><span> java.sql.SQLException; </span><span>/*</span><span> * **Mysql连接** * * 参数: * conn 连接 * url mysql数据库连接地址 * user 数据库登陆账号 * password 数据库登陆密码 * 方法: * conn 获取连接 </span><span>*/</span> <span>public</span> <span>class</span><span> MySQLConnection { </span><span>public</span> <span>static</span> Connection conn = <span>null</span><span>; </span><span>public</span> <span>static</span> String driver = "com.mysql.jdbc.Driver"<span>; </span><span>public</span> <span>static</span> String url = "jdbc:mysql://127.0.0.1:3306/post"<span>; </span><span>public</span> <span>static</span> String user = "root"<span>; </span><span>public</span> <span>static</span> String password = "123"<span>; </span><span>/*</span><span> * 创建Mysql数据连接 第一步:加载驱动 Class.forName(Driver) 第二步:创建连接 * DriverManager.getConnection(url, user, password); </span><span>*/</span> <span>public</span><span> Connection conn() { </span><span>try</span><span> { Class.forName(driver); } </span><span>catch</span><span> (ClassNotFoundException e) { System.out.println(</span>"驱动加载错误"<span>); e.printStackTrace(); } </span><span>try</span><span> { conn </span>=<span> DriverManager.getConnection(url, user, password); } </span><span>catch</span><span> (SQLException e) { System.out.println(</span>"数据库链接错误"<span>); e.printStackTrace(); } </span><span>return</span><span> conn; } }</span>
Work.java
<span>package</span><span> com.mysqltest; </span><span>import</span><span> java.sql.Connection; </span><span>import</span><span> java.sql.PreparedStatement; </span><span>import</span><span> java.sql.ResultSet; </span><span>import</span><span> java.sql.SQLException; </span><span>/*</span><span> * mysql增删改查 </span><span>*/</span> <span>public</span> <span>class</span><span> Work { </span><span>/*</span><span> * insert 增加 </span><span>*/</span> <span>public</span> <span>static</span> <span>int</span><span> insert() { MySQLConnection connection </span>= <span>new</span><span> MySQLConnection(); Connection conns; </span><span>//</span><span> 获取连接</span> PreparedStatement pst; <span>//</span><span> 执行Sql语句</span> <span>int</span> i = 0<span>; String sql </span>= "insert into user (username,password) values(?,?)"<span>; </span><span>try</span><span> { conns </span>=<span> connection.conn(); pst </span>=<span> conns.prepareStatement(sql); pst.setString(</span>1, "lizi"<span>); pst.setString(</span>2, "123"<span>); i </span>=<span> pst.executeUpdate(); pst.close(); conns.close(); } </span><span>catch</span><span> (SQLException e) { System.out.println(</span>"数据写入失败"<span>); e.printStackTrace(); } </span><span>return</span><span> i; } </span><span>/*</span><span> * select 写入 </span><span>*/</span> <span>public</span> <span>static</span> <span>void</span><span> select() { MySQLConnection connection </span>= <span>new</span><span> MySQLConnection(); Connection conns; </span><span>//</span><span> 获取连接</span> PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span> ResultSet rs; <span>//</span><span> 获取返回结果</span> String sql = "select * from user"<span>; </span><span>try</span><span> { conns </span>=<span> connection.conn(); pst </span>=<span> conns.prepareStatement(sql); rs </span>= pst.executeQuery(sql);<span>//</span><span> 执行sql语句</span> System.out.println("---------------------------------------"<span>); System.out.println(</span>"名字 | 密码"<span>); </span><span>while</span><span> (rs.next()) { System.out.println(rs.getString(</span>"username") + " | " + rs.getString("password"<span>)); } System.out.println(</span>"---------------------------------------"<span>); conns.close(); pst.close(); rs.close(); } </span><span>catch</span><span> (SQLException e) { System.out.println(</span>"数据查询失败"<span>); e.printStackTrace(); } } </span><span>/*</span><span> * update 修改 </span><span>*/</span> <span>public</span> <span>static</span> <span>int</span><span> update() { MySQLConnection connection </span>= <span>new</span><span> MySQLConnection(); Connection conns; </span><span>//</span><span> 获取连接</span> PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span> <span>int</span> i = 0<span>; String sql </span>= "update user set password = ? where username = ?"<span>; </span><span>try</span><span> { conns </span>=<span> connection.conn(); pst </span>=<span> conns.prepareStatement(sql); pst.setString(</span>1, "123"<span>); pst.setString(</span>2, "lizi"<span>); i </span>=<span> pst.executeUpdate(); pst.close(); conns.close(); } </span><span>catch</span><span> (SQLException e) { System.out.println(</span>"数据修改失败"<span>); e.printStackTrace(); } </span><span>return</span><span> i; } </span><span>/*</span><span> * delete 删除 </span><span>*/</span> <span>public</span> <span>static</span> <span>int</span><span> delete() { MySQLConnection connection </span>= <span>new</span><span> MySQLConnection(); Connection conns; </span><span>//</span><span> 获取连接</span> PreparedStatement pst; <span>//</span><span> 执行Sql语句(Statement)</span> <span>int</span> i = 0<span>; String sql </span>= "delete from user where username = ?"<span>; </span><span>try</span><span> { conns </span>=<span> connection.conn(); pst </span>=<span> conns.prepareStatement(sql); pst.setString(</span>1, "lizi"<span>); i </span>=<span> pst.executeUpdate(); pst.close(); conns.close(); } </span><span>catch</span><span> (SQLException e) { System.out.println(</span>"数据删除失败"<span>); e.printStackTrace(); } </span><span>return</span><span> i; } </span><span>/*</span><span> * test </span><span>*/</span> <span>public</span> <span>static</span> <span>void</span><span> main(String[] args) { </span><span>//</span><span> System.out.println(insert());</span> <span> select(); </span><span>//</span><span> System.out.println(update()); </span><span>//</span><span> System.out.println(delete());</span> <span> } }<br /><br /></span>
test screenshot

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools