JSP connection to Mysql database details and Instances

Source: Internet
Author: User

1. Preparations
Well, the first step is to download the JDBC driver, we can go to the http://dev.mysql.com/downloads/connector/j/3.1.html download. The latest JDBC driver version is 3.1.10. The download file is: mysql-connector-java-3.1.10.zip. Directly decompress the package. We only need mysql-connector-java-3.1.10-bin. jar file and mysql-connector-java-3.1.10-bin-g.jar file, put the two files somewhere, we can put in the installation directory (such as: C: j2sdklib. The next step is to set the environment variable and set the value of classpath:

.; C: j2sdklibools. jar;
C: j2sdklibmysql-connector-java-3.1.10-bin.jar;
C: j2sdklibmysql-connector-java-3.1.10-bin-g.jar

After configuring the environment variables, there is a very important step is to configure the driver for JSP Connection database, this is very simple, is the mysql-connector-java-3.1.10-bin. copy the jar file to the following folder (assume that your Tomcat file is in the C drive and directory: C: Tomcat 5.0)

C: Tomcat 5.0 commonlib


2. Create a database
For more information about downloading and installing Mysql databases, see http://www.numb1.com/com/php/phpsetup.htm.

The next step is to create a database. First, enter mysql from CMD (DOS) and create a database. The command is as follows:
Mysql> create database numb1;

Enter the new database:
Mysql> use numb1

Create a table:
Mysql> create table 'lyb '(
'Id' int (11) not null auto_increment,
'Name' varchar (20) default NULL,
'Mail' varchar (30) default NULL,
'Qq' varchar (15) default NULL,
'Txt 'text,
Primary key ('id ')
);

Insert data
Mysql> insert into lyb values (, cream, root@numb1.com, 191106245, hello, leave a message for you, huh, huh );
Mysql> insert into lyb values (, lekai, jq_1979@numb1.com, 11225032, I would also like to say );

Then
Mysql> exit
C:> exit
OK.

3. Connect to the database
The following step is the most exciting part of this article. We have prepared for the next step, that is, connecting to the database. You can put the following code village into the *. jsp file and run it on TOMCAT to check the result.

<% @ Page contentType = "text/html; charset = GBK" language = "java" import = "java. SQL. *" errorPage = "" %>
<Center> <B> if you see the content in the following table, the database is connected successfully! </B> </center> <br>
<Table width = "720" border = "1" align = "center" cellspacing = "0">
<Tr>
<Th> ID </th>
<Th> nickname </th>
<Th> email </th>
<Th> QQ </th>
<Th> message content </th>
</Tr>
<%
Class. forName ("com. mysql. jdbc. Driver"). newInstance (); // org. gjt. mm. mysql. Driver
String u = "jdbc: mysql: // localhost/numb1? User = root & password = "; // useUnicode = true & characterEncoding = gb2312 ";
Java. SQL. Connection con = java. SQL. DriverManager. getConnection (u );
Java. SQL. Statement statement = con. createStatement ();
String SQL = "select * from lyb ";
Java. SQL. ResultSet rs = statement.exe cuteQuery (SQL );

While (rs. next () {%>
<Tr>
<Td> <div align = "center"> <% = rs. getString (1) %> </div> </td>
<Td> <div align = "center"> <% = rs. getString (2) %> </div> </td>
<Td> <div align = "center"> <% = rs. getString (3) %> </div> </td>
<Td> <div align = "center"> <% = rs. getString (4) %> </div> </td>
<Td> <div align = "center"> <% = rs. getString (5) %> </div> </td>
</Tr>
<% }%> </Table>
<% Out. print ("<br> <center> database operation successful </center>"); %>
<%
Rs. close ();
Statement. close ();
Con. close ();
%>

A few simple descriptions
Class. forName ("com. mysql. jdbc. Driver"). newInstance (); // org. gjt. mm. mysql. Driver
Com. mysql. jdbc. Driver can be replaced by org. gjt. mm. mysql. Driver.
String u = "jdbc: mysql: // localhost/numb1? User = root & password = ";

Make sure your database name is numb1, and the database username is: root (default system). The database password is blank.

The connection to the database ends here. Another problem is that when you run the above program, the running result may be garbled, because of the difference between JAVA encoding and Mysql. We can solve this problem using the following methods.

4. garbled Processing
Method 1:
Modify String u = "jdbc: mysql: // localhost/numb1? User = root & password = ": String u =" jdbc: mysql: // localhost/numb1? User = root & password = & useUnicode = true & characterEncoding = gb2312 ";
If the MySQL version is later than 4.1, you can add "useUnicode = true & characterEncoding = gb2312" without adding it. If the problem cannot be solved, we will adopt the second method.

Method 2:
Define a function first
<%!
Public String getStr (String s ){
String str = s;
Try {
Byte B [] = str. getBytes ("ISO-8859-1 ");
Str = new String (B );
Return str;
}
Catch (Exception e) {return null ;}
}
%>
We use this function to process the data read from the database. The Code is as follows:

<% @ Page contentType = "text/html; charset = GBK" language = "java" import = "java. SQL. *" errorPage = "" %>
<%!
Public String getStr (String s ){
String str = s;
Try {
Byte B [] = str. getBytes ("ISO-8859-1 ");
Str = new String (B );
Return str;
}
Catch (Exception e) {return null ;}
}
%> <Center> <B> if you see the content in the following table, the database is connected successfully! </B> </center> <br>
<Table width = "720" border = "1" align = "center" cellspacing = "0">
<Tr>
<Th> ID </th>
<Th> nickname </th>
<Th> email </th>
<Th> QQ </th>
<Th> message content </th>
</Tr>
<%
Class. forName ("com. mysql. jdbc. Driver"). newInstance (); // org. gjt. mm. mysql. Driver
String u = "jdbc: mysql: // localhost/numb1? User = root & password = "; // useUnicode = true & characterEncoding = gb2312 ";
Java. SQL. Connection con = java. SQL. DriverManager. getConnection (u );
Java. SQL. Statement statement = con. createStatement ();
String SQL = "select * from lyb ";
Java. SQL. ResultSet rs = statement.exe cuteQuery (SQL );

Now, the display is complete. If the display still fails, check other settings carefully. Or consult relevant materials.

Message Board (recommended): http://www.numb1.com/download/download.php? Id = jm1

While (rs. next () {%>
<Tr>
<Td> <div align = "center"> <% = getStr (rs. getString (1) %> </div> </td>
<Td> <div align = "center"> <% = getStr (rs. getString (2) %> </div> </td>
<Td> <div align = "center"> <% = getStr (rs. getString (3) %> </div> </td>
<Td> <div align = "center"> <% = getStr (rs. getString (4) %> </div> </td>
<Td> <div align = "center"> <% = getStr (rs. getString (5) %> </div> </td>
</Tr>
<% }%> </Table>
<% Out. print ("<br> <center> database operation successful </center>"); %>
<%
Rs. close ();
Statement. close ();
Con. close ();
%>

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.