In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to connect MySQL with PHP and JSP under Win2000". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to connect MySQL with PHP and JSP under Win2000".
Before reading this document, you should have installed Apache, JDK, Tomcat, PHP and MySQL under your Win2000. If you have not successfully installed these software, you can refer to the use of Apache1.3.20 under Windows, the installation of JDK1.3.1 under Windows, the installation of Tomcat under Win2000, the installation of PHP under Win2000, and the installation of MySQL under Win2000.
In this document, we are not going to explain too much about PHP, MySQL, JSP, we will assume that you have a rough grasp of PHP, MySQL, JSP. We are just looking at how to connect to MySQL using PHP and JSP under Win2000.
II. Preparation for MySQL
In our previous installation of MySQL, we only used the initial root user of the system. In this article, we are not going to continue to use root users, although we are working in an experimental environment, but it is important to develop a good habit, because what we are likely to face in the future is a completely open Internet. As in most cases, the abuse of root users in MySQL can lead to the collapse of MySQL.
The initial situation after MySQL installation is a little strange. In order to add users and set user permissions correctly, it is necessary to clean up the tables User and DB in the mysql library.
First of all, enter the c:/mysql/bin directory from the command line, type mysql directly, and you will enter the MySQL client program MYSQL (below, we use MYSQL to refer to the MySQL client program, while in other cases, we will use MySQL). Enter at the "mysql >" prompt:
Show databases
You can see that in the initial state, there are two databases under MySQL: mysql and test,test libraries are of no use to us anyway, and keeping them will confuse the public, so we might as well delete them:
Drop database test
About the users and their permissions saved in the mysql database, the key is the db table and the user table. Now, we can clear the db table first:
Use mysql
Delete from db
Then we clean up the user library and refresh it:
Delete from user where user='' or host='localhost'
Flush privileges
Now, exit MYSQL with quit.
Now, you won't be able to enter MYSQL directly by typing mysql (in that case, you won't actually need user authentication to enter MYSQL). You can only enter mysql-u root-p.
If you have previously set a password for root users, you will be prompted to enter your password. If you have not already set it, press enter to enter MYSQL.
The existence of root users without a password is a terrible situation. You'd better exit the client program and use the method we described in the installation of MySQL under Win2000 to set a password for root before entering MYSQL.
We set up a new database for use:
Create database my_test
Create a data table in this library:
Use my_test
Create table my_test_table (test_column char (20) not null)
Add a row to this table:
Insert my_test_table values ('Hello,I'm MySQL databases')
Now let's create a new user yzysy with the grant command, which only has SELECT, UPDATE, INSERT, and DELETE permissions on the my_test library. The user's password is also yzysy:
Grant SELECT,UPDATE,INSERT,DELETE on my_test.* to yzysy identified by 'yzysy'
For more information on the grant command, see the MySQL user's manual.
3. PHP connects to MySQL
PHP is very easy to connect to MySQL, and we don't need to make any settings.
Create a new file MySQL.php in the htdocs directory of the Apache installation directory:
PHP connects to MySQL!
Where mysql_connect () is used to connect to the database server, mysql_select_db () is used to select the working database, mysql_query () is used to execute SQL queries, and mysql_fetch_array () is used to obtain the result set of select.
Now, open your browser and type http://localhost/MySQL.php in the address bar, and you should see "Hello,I 'm MySQL!" in boldface.
, JSP connects to MySQL
Connecting JSP to MySQL is a little more complicated.
First you must download mm.mysql.jdbc-1.2c.tar.gz from http://www.mysql.com/Downloads/Contrib/mm.mysql.jdbc-1.2c.tar.gz, and then extract it to a directory on your local hard drive (we are using c:/mm.mysql.jdbc-1.2c.
Then, at the end of the CLASSPATH system variable, add corexplash mm.mysql.JDdbcMur1.2c; (here / it should be a backslash)
Create a JavaBean called DBconn.java, create a mysqltest directory under / webapps/examples/WEB-INF/classes of Tomcat, and save the file in this directory, and DBconn.java is used to encapsulate the operation of linking to the database. The DBconn.java is as follows:
Package Mysqltest
Import java.sql.*
Public class DBconn
{String DBDriver= "org.gjt.mm.mysql.Driver"
String ConnStr= "jdbc:mysql://localhost/my_test"
String MyUser= "yzysy"
String MyPassword= "yzysy"
Connection conn = null
ResultSet rs = null
Public DBconn ()
{try
{Class.forName (DBDriver)
}
Catch (java.lang.ClassNotFoundException e)
{System.err.println ("DBconn ():" + e.getMessage ())
}
}
Public ResultSet executeQuery (String sql)
{rs = null
Try
{conn = DriverManager.getConnection (ConnStr,MyUser,MyPassword)
Statement stmt = conn.createStatement ()
Rs = stmt.executeQuery (sql)
}
Catch (SQLException ex)
{System.err.println ("aq.executeQuery:" + ex.getMessage ())
}
Return rs
}
}
We noticed that in this program, only
String DBDriver= "org.gjt.mm.mysql.Driver"
String ConnStr= "jdbc:mysql://localhost/my_test"
With the characteristics of MySQL, the rest of the programs are no different from other JDBC applications.
Use the Javac command of JDK to compile DBconn.java to form the corresponding class file.
Create the Mysqltest.jsp file in the / webapps/examples/jsp directory of Tomcat. Its contents are as follows:
JSP connects to MySQL!
You can't see any traces of MySQL here.
Now type http://localhost/examples/jsp/Mysqltest.jsp in the browser's address bar, and you should see "Hello,I 'm MySQL!" in boldface.
5. Conclusion
Now, you have successfully built a zero-cost Web server under your Win2000. However, I personally think that in Win2000 can only be used as a development environment, really put into use, and the use of linux can get more performance and security guarantee. [@ more@]
Undefinedundefined
Thank you for your reading, the above is the content of "how to connect MySQL with PHP and JSP under Win2000". After the study of this article, I believe you have a deeper understanding of the problem of how to connect MySQL with PHP and JSP under Win2000. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.