MySQL, JDBC, Driver

Source: Internet
Author: User

JDBC name: connectors/J 3.0
URL: http://www.mysql.com/
Version: 3.0.8-stable
Download URL: http://www.codepub.com/d/tag.php? N = 1 & tag = JDBC
Syntax
Class. forname ("com. MySQL. JDBC. Driver ");
Connection con = drivermanager. getconnection ("JDBC: mysql: // host: Port/Database", "user", "password ");
Programming
1. Use drivermanager to register mm. MySQL
The Class Name of MM. MySQL is org. gjt. Mm. MySQL. Driver. It must be written during registration.
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
2. jdbc url parameter description
URL format: JDBC: mysql: // [hostname] [: Port]/dbname [? Param1 = value1] [? M2 = value2]...
Default parameter name
User Database User name none
Password Database User Password unavailable
Autoreconnect determines whether to automatically connect when the database connection is lost. The value is true/False false.
Maxreconnects if autoreconnect is true, this parameter indicates the number of retries. The default value is 3.
Initialtimeout if autoreconnect is true, this parameter is the number of seconds waiting before reconnection 2
Maxrows sets the number of rows returned during the query. 0 indicates all 0
Whether useunicode uses Unicode output, true/False false
Characterencoding if useunicode, this parameter is set to the encoding type. It is recommended that you use 8859_1
Prompt
At the same time, useunicode and characterencoding can be used to solve Chinese problems in database output.
For example, JDBC: mysql: // localhost/test? User = root & useunicode = true; characterencoding = 8859_1
A simple example
This JSP Example uses a library, which has only one table (Address Book)
Create Database addressbook
Use addressbook
Create Table addressbook (ID int auto_increment primary key, name varchar (30), address varchar (255), phone varchar (20 ));
Insert record
Insert into addressbook (name, address, phone) values ('jx', 'zhejiang Yuyao ', '2017-0574 ');
JSP code: absolute, resultset. type_scrool_sensitive and other directories are used in the code to test whether mm. MySQL complies with JDBC 2.0 specifications.
<% @ Page import = "Java. SQL. *" %>
<%
Out. println ("Address Book! ");
Try {
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
}
Catch (exception e ){
Out. println ("unable to load driver .");
}
Try {
Connection c = drivermanager. getconnection ("JDBC: mysql: // localhost/addressbook? User = root & Password = jjx & useunicode = true & characterencoding = 8859_1 ");
Statement S = C. createstatement (resultset. type_scroll_sensitive, resultset. concur_read_only );
Resultset rs=s.exe cutequery ("select * From addressbook ");
Out. println ("<Table border = 1> ");
Int I = 1;
For (I = 10; I <20; I ++)
{
If (Rs. Absolute (I ))
{
Out. println ("<tr> <TD> ");
Out. println (Rs. getstring (1 ));
Out. println ("</TD> ");
Out. println ("<TD> ");
Out. Print (Rs. getstring (2 ));
Out. println ("</TD> ");
Out. println ("<TD> ");
Out. Print (Rs. getstring (3 ));
Out. println ("</TD> </tr> ");
}
Else
{
Break;
}
}
Out. println ("</table> ");
Rs. Close ();
S. Close ();
C. Close ();

}
Catch (sqlexception e ){
Out. println ("sqlexception:" + E. getmessage ());
Out. println ("sqlstate:" + E. getsqlstate ());
Out. println ("vendorerror:" + E. geterrorcode ());
}
%>
 

 
Mm. MySQL is a MySQL JDBC driver of type 4 (pure Java) that complies with JDBC 2 specifications. The current version is 2.0 pre 5 (Beta)
You can from http://www.codepub.com/d/tag.php? N = 1 & tag = JDBC download the latest driver
This document briefly describes how to install and use mm. MySQL.
Casual residence editing
Install
1. Download mm. MySQL
2. decompress the package to a drive, such as C:
3. Modify the classpath and add the path of MM. MySQL. For example, your original classpath may be:
.; C: jdk1.2.2lib ools. jar; C: jdk1.2.2libdt. jar;
After modification, it may be
.; C: jdk1.2.2lib ools. jar; C: jdk1.2.2libdt. jar; C: Mm. MySQL. jdbc-2.0pre5;

Programming

1. Use drivermanager to register mm. MySQL
The Class Name of MM. MySQL is org. gjt. Mm. MySQL. Driver. It must be written during registration.
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();

2. jdbc url parameter description
URL format: JDBC: mysql: // [hostname] [: Port]/dbname [? Param1 = value1] [m2 = value2]...
Default parameter name
User Database User name none
Password Database User Password unavailable
Autoreconnect determines whether to automatically connect when the database connection is lost. The value is true/False false.
Maxreconnects if autoreconnect is true, this parameter indicates the number of retries. The default value is 3.
Initialtimeout if autoreconnect is true, this parameter is the number of seconds waiting before reconnection 2
Maxrows sets the number of rows returned during the query. 0 indicates all 0
Whether useunicode uses Unicode output, true/False false
Characterencoding if useunicode, this parameter is set to the encoding type. It is recommended that you use 8859_1

Prompt
At the same time, useunicode and characterencoding can be used to solve Chinese problems in database output.
For example, JDBC: mysql: // localhost/test? User = root & useunicode = true; characterencoding = 8859_1

A simple example
This JSP Example uses a library, which has only one table (Address Book)

Create Database addressbook
Use addressbook
Create Table addressbook (ID int auto_increment primary key, name varchar (30), address varchar (255), phone varchar (20 ));

Insert record
Insert into addressbook (name, address, phone) values ('jx', 'zhejiang Yuyao ', '2017-0574 ');

JSP code: absolute, resultset. type_scrool_sensitive and other directories are used in the code to test whether mm. MySQL complies with JDBC 2.0 specifications.
<% @ Page import = "Java. SQL. *" %>
<%
Out. println ("Address Book! ");

Try {
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();

}
Catch (exception e ){
Out. println ("unable to load driver .");

}

Try {
Connection c = drivermanager. getconnection ("JDBC: mysql: // localhost/addressbook? User = root & Password = jjx & useunicode = true & characterencoding = 8859_1 ");
Statement S = C. createstatement (resultset. type_scroll_sensitive, resultset. concur_read_only );
Resultset rs=s.exe cutequery ("select * From addressbook ");
Out. println ("<Table border = 1> ");
Int I = 1;
For (I = 10; I <20; I ++)
{
If (Rs. Absolute (I ))
{
Out. println ("<tr> <TD> ");
Out. println (Rs. getstring (1 ));
Out. println ("</TD> ");
Out. println ("<TD> ");
Out. Print (Rs. getstring (2 ));
Out. println ("</TD> ");
Out. println ("<TD> ");
Out. Print (Rs. getstring (3 ));
Out. println ("</TD> </tr> ");
}
Else
{
Break;
}

}
Out. println ("</table> ");
Rs. Close ();
S. Close ();
C. Close ();

}
Catch (sqlexception e ){
Out. println ("sqlexception:" + E. getmessage ());
Out. println ("sqlstate:" + E. getsqlstate ());
Out. println ("vendorerror:" + E. geterrorcode ());
}

%>
This article from: Development Institute http://edu.codepub.com Source: http://edu.codepub.com/2009/0408/2621.php

Related Article

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.