Simple application using JDBC technology to connect database (source code)--java

Source: Internet
Author: User

I. Creating a database (take MySQL database as an example)MySQL database download installation and configuration-refer to Blogger's essay before: Build MySQL database under Windows platformCreate WXB databases-create database wxb;Use the-show databases statement to view all database names under the MySQL databasetwo. Connect to MySQL database using JDBC technologyJAVA=============JDBC===============DB high-level programming language Structured Query Language Jdbc:java database Connectivity-java and DB connection JDBC core is: six Footwork, six elements and database connections need to use the first two steps in the six-step: Load Driver--Create a connection six elements: Drivertype, ServerName, Port, database name, UserName, Password (driver type, server name , port number, database name, user name, password) 1. First Step: Load driver Add database jar package to project, use Class.forName () method to load driver class driver-> in COM.MYSQL.JDBC package with Try ... Catch catch exception to ensure that the main class of the database jar package is validdatabase jar package Download: Jar.zip2. Step Two: Create a connection through the method getconnection () in the DriverManager class, connect to the database using the six-factor wxb to catch the exception with Try...catch. The code is as follows:Package cn.nxl123.www;
import Java.sql.DriverManager;
import java.sql.SQLException;
Public class Connect {
Public static void Main (string[] args) {
String classname= "Com.mysql.jdbc.Driver";
try {
Class.forName (className);//Load driver by forname method
System.out.println ("Load driver succeeded!") ");
} catch (ClassNotFoundException e) {
//used to catch exceptions
System.out.println ("Load driver failed!") ");
e.printstacktrace ();
}
String url= "jdbc:mysql://localhost:3306/wxb?usessl=false";//Four elements
String user= "root";//The following are two elements, root is the blogger computer MySQL user name
String password= "123456";//Blog master mysql user password set to 123456
try {
method Getconnection () in the//drivermanager class, connecting to the database with six elements WXB
drivermanager.getconnection (URL, user, password);
System.out.println ("Connection database succeeded! ");
} catch (SQLException e) {
//Second catch exception
System.out.println ("Connection database failed! ");
e.printstacktrace ();
}
}
}Connection Successful:

Source: Jdbc-dba.zipNote: The article for my original reprint please indicate the source of the article will inevitably have flaws in the place also hope that we have a lot of comments or suggestions can contact me qq:2187093468 (non-prudential do not Disturb, add friends please note where to see)

Simple application using JDBC technology to connect database (source code)--java

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.