Jsp connection to mysql database

Source: Internet
Author: User
I think it is an urgent question for beginners to try to connect jsp to the database. In fact, the connection to the database will be involved in website construction in the future. Mysql is connected here. I chose Mysql because I like open-source Mysql, which is unrelated to its platform and small but functional. Is a very good

I think it is an urgent question for beginners to try to connect jsp to the database. In fact, the connection to the database will be involved in website construction in the future. Mysql is connected here. I chose Mysql because I like open-source Mysql, which is unrelated to its platform and small but functional. Is a very good

I think it is an urgent question for beginners to try to connect jsp to the database. In fact, the connection to the database will be involved in website construction in the future. Mysql is connected here. I chose Mysql because I like open-source Mysql, which is unrelated to its platform and small but functional. Is a very good choice.

First, make sure that Mysql has been installed on the machine.

The previous job was to download its driver package from the Mysql official website for free.

Copy it to the common/lib directory of tomcat. (The test server I used is tomcat)

Then write the following code to test whether the connection is normal.

<%java.sql.Connection conn;
java.lang.String strConn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

conn= java.sql.DriverManager.getConnectio("jdbc:mysql://localhost/test","root","");

%>

Save it as a jsp file and place it in the directory G: \ Apache Software Foundation \ tomcat 5.0 \ webapps \ ROOT of Tomcat. Then, view the page in the browser. If no exception is reported, A blank page is displayed, indicating that the connection is correct. next, we will test the connection to mysql.

We can write all the code into a class, as shown in the following code:

Public class
{
Public static void main (String [] args)
{

String str = null ;//
Statement stmt = null;
Connection conn = null;
Try
{
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
// Load the driver
String url = "jdbc: mysql: // localhost: 3306/qqnumber ";
String user = "root ";
String password = "******";
String SQL = "select * from qqNumber ";//
Conn = DriverManager. getConnection (url, user, password); // establish a connection
Stmt = conn. createStatement

(ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
ResultSet rs1_stmt.exe cuteQuery (SQL); // The ResultSet API used to obtain the data result set

// Provide access to the data table. The ResultSet object is usually generated by executing the "statement ".
Rs. next ();
Str = rs. getString ("number ");
System. out. println (str );
System. out. println ("database operation successful, congratulations ");
Rs. close ();
}
Catch (Exception e)
{
System. out. println (e );
}
Finally
{
If (stmt! = Null)
{
Try
{
Stmt. close ();
}
Catch (SQLException e)
{
System. out. println (e );
}
}
If (conn! = Null)
{
Try
{
Conn. close ();
}
Catch (SQLException e)
{
System. out. println (e );
}
}
} // Finally
} // Main ()

}

The error I encountered during the first debugging.

Com. mysql. jdbc. UpdatableResultSet @ 1cb25f1 database operation successful. Congratulations, read the following sentence. Obviously, this operation has been successfully run! However, the data query results I want to display are not displayed.

The problem lies in the ResultSet rs1_stmt.exe cuteQuery (SQL); the problem of the data result set obtained. to display it, we must convert it into a string.

The following problem occurs: java. SQL. SQLException: Before start of result set because ResultSet always has a cursor pointing to its current data row. Initially, the cursor is positioned before the first line. The next () method moves the cursor to the next row. This error is reported when you forget to write the next method.

(Responsible editor: Ming mingming_ky@126.com TEL :( 010) 68476636)

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.