Jsp connection to mysql database, jspmysql Database

Source: Internet
Author: User

Jsp connection to mysql database, jspmysql Database

1. Create a Java web project.

2. Import the mysql driver package. (This is similar to the Java connection to mysql written in the previous article)

3. Write test code

<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %>

<% @ Page language = "java" %>

<% @ Page import = "com. mysql. jdbc. Driver" %>

<% @ Page import = "java. SQL. *" %>

<%
String driverName = "com. mysql. jdbc. Driver ";
String userName = "root ";
String userPasswd = "root ";
String dbName = "goods ";
String tableName = "t_admin ";
String url = "jdbc: mysql: // localhost/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection connection = DriverManager. getConnection (url );
Statement statement = connection. createStatement ();
String SQL = "SELECT * FROM" + tableName;
ResultSet rs = statement.exe cuteQuery (SQL );
ResultSetMetaData rmeta = rs. getMetaData ();
Int numColumns = rmeta. getColumnCount ();
Out. print ("id ");

Out. print ("| ");

Out. print ("num ");

Out. print ("<br> ");

While (rs. next ()){

Out. print (rs. getString (1) + "");

Out. print ("| ");

Out. print (rs. getString (2 ));

Out. print ("<br> ");

}

Out. print ("<br> ");

Out. print ("database operation successful, congratulations ");

Rs. close ();

Statement. close ();

Connection. close ();

%>

4. Copy the mysql driver package to the lib directory of tomcat.

5. Open your browser and run the test file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.