Table of Contents
PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql
Home Backend Development PHP Tutorial 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_PHP tutorial

Jul 12, 2016 am 09:04 AM
java mysql php and study database notes Script connect

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?<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>?>

Copy after login

test.php test

1

2

3

4

5

6

7

8

9

10

11

12

<?<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>?>

Copy after login

Running screenshot: PHP study notes - PHP script and JAVA connect to mysql database, study notes mysql_PHP tutorialjava connection method

1. Create a new java project as mysqlTest

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

MySQLConnection.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<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>

Copy after login

Work.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

<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>

Copy after login

test screenshot

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1074251.htmlTechArticlePHP 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, j...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP: Creating Interactive Web Content with Ease PHP: Creating Interactive Web Content with Ease Apr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

See all articles