Use PHP and JSP to connect with PHP and JSP in Win2000
-------------------------------------------------- ------------------------------ First, before reading this document, your Win2000 should have installed apache, JDK, Tomcat, PHP, MySQL. In this document, we don't prepare too much explanation for PHP, MySQL, JSP, we will assume that you have roughly master PHP, MySQL, JSP. We only explore the method of connecting MySQL using PHP and JSP under Win2000. Second, Mysql's pre-preparatory mysql installed before we installed the system initial root users. In this article, we are not prepared to continue to use root users, although we work in a test environment, but developing a good habit is crucial, because we will face it in the future will be a fully open Internet. As in most cases, the abuse of root users in MySQL may also cause mysql crash. The initial situation after mysql installation is a bit weird, to correctly increase the user and set up user permissions, we must clean up the table USER and DB in the MySQL library. First, enter the c: / mysql / bin directory in the command line mode, enter mysql directly, you will enter the MySQL client program mysql (below, we use mysql refer to MySQL client program, and in other occasions, we Mysql will be used. Enter: Show Databases after "MySQL>" prompt; you will see the initial state, there are two databases under MySQL: MySQL and Test, the Test Library is not used by us, and it will confuse the audio, so we You may wish to delete it: DROP DATABASE TEST; related users and their permissions save in the MySQL database, where the key is the DB table and user table, now we can clear the DB table first: use mysql; delete from DB; then we are for User The library is cleaned and refreshed: delete from user where user = '' or host = 'localhost'; flush privileges; now, quit MySQL with Quit. Now, you will not be able to enter Mysql directly into MySQL (that kind of situation does not require user authentication into mysql). You can only enter mysql -u root -p. If you have a password for the root user, you will be prompted to enter your password. If you haven't set it yet, press the Enter key directly to enter mysql. There is a terrible situation that there is no password, you'd better exit the client program with our method we describe in the installation of MySQL in WIN2000 to set a password after root and enter mysql. We create 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; In this table: Insert My_Test_Table VALUES ('Hello, I'm MySQL!'); Now we use the grant command to create a new user yzysy, which only has SELECT, UPDATE, INSERT, and DELETE permission to my_test.
The user's password is also YZYSY: GRANT SELECT, UPDATE, INSERT, DELETE ON MY_TEST. * To yzysy Identified by 'yzysy'; see the MYSQL User Manual for a detailed description of the GRANT command. Third, PHP connection MySQLPHP connection mysql is very simple, we don't need to make any settings. Create a new file mysql.php:
Fourth, JSP connection MySQL JSP connection MySQL is a bit more complicated. First you have to download mm.mysql.jdbc-1.2c.tar.gz from http://www.mysql.com/downloads/contrib/mm.mysql.jdbc-1.2c.gz, then decompress it to the local In a directory of the hard disk (we use C: /MM.Mysql.jdbc-1.2c. Then add C: /mm.mysql.jdbc-1.2c at the last addition of the ClassPath system variable; (here / should be counterclockwise) Bar) Create a JavaBean called dbconn.java, build a mysqltest directory under Tomcat / WebApps / Examples / WEB-INF / CLASSES, save the file in this directory, dbconn.java is used to package the database link Operation. 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 this program is only string dbdriver = "org.gjt.mm.mysql.driver"; string connStr = "jdbc: mysql: // localhost / my_test"; features with MySQL, the rest of the program There is no difference from other JDBC applications. Compile DBConn.java to form a corresponding Class file with JDK's Javac command. Built a mysqltest.jsp file in Tomcat's / WebApps / Examples / JSP directory.
The content is as follows: <% @ Page ContentType = "text / html; charset = GB2312"%>