"Guide" This article for beginners about JSP connection MySQL database problem.
I think it is an urgent matter for beginners to try to connect the JSP with the database. In fact, in the future when doing the website, all involve the connection with the database. I'm connected to MySQL here. The reason I choose MySQL, because I like the open source of it, and its platform independent, and small but not without losing functionality. is a very good choice.
First make sure that MySQL is installed on the machine.
Before doing the job is to get the official download of its driver package in MySQL, which can be obtained free of charge.
Copy it to Tomcat's common/lib directory; (my test server is tomcat5.0)
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 as a JSP file, then put it in the Tomcat G:\Apache Software foundation\tomcat 5.0\webapps\root directory, and then view the page in the browser, if not abnormal, display a blank page, Indicates that the connection has been properly connected. The next job is to test the connection to MySQL.
We can simply write all the code into a class. The following code shows:
public class as
{
public static void Main (string[] args)
{
String Str=null; //
Statement Stmt=null;
Connection Conn=null;
Try
{
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Load Driver
String url= "Jdbc:mysql://localhost:3306/qqnumber";
String user= "root";
String password= "Hu Jintao";
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 rs=stmt.executequery (SQL); Get data result set ResultSet interface
Provides access to a data table. ResultSet objects are usually generated by executing "statements"
Rs.next ();
Str=rs.getstring ("number");
System.out.println (str);
SYSTEM.OUT.PRINTLN ("Successful database operation, 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 the first time I was debugging.
COM.MYSQL.JDBC.UPDATABLERESULTSET@1CB25F1 database operation successful, congratulations, look at the back of this sentence, it is obvious that this has been successfully run! But the data query results I want to display are not shown.
The problem is ResultSet rs=stmt.executequery (SQL); The problem of the collection of data results obtained. If we want to show it, we have to turn it into a string.
The following problem java.sql.SQLException:Before the start of result set because ResultSet always has a cursor pointing to its current data row. Initially, the cursor is positioned in front of the first row. The next () method moves the cursor to the next line. This error is reported when you forget to write the next method.