Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Probing into the connection between Jsp and Mysql to check errors

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. Preface

Before reading this document, you should have installed Apache, JDK, Tomcat, PHP, MySQL. 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 only discussing the method of using PHP and JSP to connect MySQL 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 very important to develop a good habit, because what we will face in the future may be 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 correctly increase users and set user rights, 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 on other occasions, 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 will not be able to enter MYSQL directly by typing mysql (which actually requires no 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 we use the grant command to create a new user yzysy, 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 about the grant command, please refer to the MySQL user's manual.

Third, use PHP to connect MYsql omitted

4. 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 named DBconn.java, create a mysqltest directory under / webapps/examples/WEB-INF/classes of Tomcat, and save the file in this directory. The contents of DBconn.java to encapsulate the operation linked to the database. DBconn.java are 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 program is 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:

You can't see any trace 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 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. (End)

* * *

In fact, that's the wrong point, the direction of the Mysql driver, I did what it said, and as a result, Tomcat always reported that there was no suitable driver.

In order to verify the correctness of the directory pointing to the target, I put the "Super Horsepower Brothers" game into this directory and enter the name of the "Super horsepower Brothers" game in the DOS window, and the game can be found, indicating that the CLASSPATH setting is correct, that is to say, the JSP server did not come here to find it, no matter how hard I tried, it was useless. From then on, the nightmare became my good friend.

After I reinstalled XPServer, I copied all the ORG directories in the mm.mysql.jdbc-1.2c directory to Tomcat40CLASSES. Unexpectedly, I succeeded:

All http://vip.6to23.com/wocienyoung provided by novelty World iNENS

Because the driver is String DBDriver= "org.gjt.mm.mysql.Driver", it is necessary to test the entire directory of ORG, so that the whole connection debugging is completed, JSP is really used everywhere, (by the way, I use Resin under Linux, Export Classprth directly find the driver of Mysql in Profile) it seems that the environment is different, what may be different, write out here hope to be helpful to those who are looking for ways.

[@ more@]

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report