JDBC introduction (2)

xiaoxiao2021-03-06  40

Connectdb

This article is an example of accessing the mysql database, explaining how JDBC connects Database>

In order to connect to MySQL Database, there must be a MySQL JDBC driver, add the downloaded mysql-connector-java- *. Jar to ClassPath, first demonstrate how to connect Mysql in Java Class, Java Class API is located in java.sql package .

To connect mysql, several actions must be:

Loading and registration driver

Load and log up the JDBC driver through the java.lang.class package () to the DriverManager (the driver will automatically register with DriverManager.RegisterDriver () method), MySQL driver category is com.mysql.jdbc.driver, reference The following code:

Try {class.Forname ("com.mysql.jdbc.driver);} catch (classnotfoundexception e) {system.out.println (" not find the mysql database driver);}

If you can't find com.mysql.jdbc.driver, you will throw the classnotfoundexception, which is to determine if you have the location of MySQL-Connector-Java- *. Jar in your classpath.

. Provide JDBC URL

JDBC URL defines the agreement, subtraction, information source, etc. when connecting the database:

Agreement: Sub-Agreement: Source Category Agreement is always the beginning of JDBC in JDBC; subtraction is a connected driver or database management system name. If you use mysql, you are mysql; Data Source Category Represents the location of the database source; mysql The format of JDBC URL is as follows:

JDBC: mysql: // Host Name: Connection / Database Name? Parameter 1 = Value 1 & Parameter 2 = Value 2

The host name can be local localhost or other network connection hosts, connecting to 3306, if you want to connect to the GuseTBook database, and specify the name and password of the access:

JDBC: mysql: // localhost: 3306 / GuestBook? User = myname & password = 123456

If you want to use Chinese access, you must also give parameters USERUNICODE and CHARACTERENCODING, indicating whether Unicode is used, and is natively located, for example:

JDBC: mysql: // localhost: 3306 / Guestbook? user = myname & password = 123456 & usrunicode = true & characterencoding = GB18030

Gets from DriverManager Connection

To connect to the database, we can request and get the Connection connection string from DRIVERMANAGER, you can directly give it a JDBC URL as a parameter and retrieve the Connection connection string

try {String url = "jdbc: mysql: // localhost: 3306 / GUSTBOOK?" "user =" strusername "& password" strpassword; Connection conn = DriverManager.getConnection (url); if (conn.isClosed ()! ) System.out.println ("Connection SucessFully"); conn.close ();} catch (sqlexception e) {....

After obtaining the Connection connection string, you can test whether the connection to the database is turned off (isclosed ()). GetConnction () method also provides a given user name and password to define each parameter; for example:

String url = "jdbc: mysql: // localhost: 3306 / gustbook"; string user = "myname"; string password = "123456"; connection conn = drivermanager.getConnction (URL, User, Password); Database (DBConnectionDemo.java)

package onlyfun.mydbconnection; import java.sql *; public class DBConnectionDemo {public static void main (String [] args) {String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:. mysql: // localhost: 3306 / GUSTBOOK "; String username =" myname "; String password =" 123456 "; try {Class.forName (driver); Connection conn = DriverManager.getConnection (url, username, password); if (conn.isClosed! ()) System.out.Println ("Database Connection Success"); conn.close ();} catch (classnotfoundexcection e) {system.out.println ("Can't find the specified driver:" driver);} catch (SQLEXCEPTION E) {E.PrintStactTrace ();}}

转载请注明原文地址:https://www.9cbs.com/read-58512.html

New Post(0)