before manipulating Data
1) Log in to the database server (mysql-u root-p password) via MySQL client tool
2) Writing SQL statements
3) Send SQL statement to database server execution
What is jdbc?
the technique of sending SQL statements using Java Code (Programs) is jdbc technology!!!!
using jdbc to send sql Prerequisites
- Log on to the database server (connect to the database server)
- of the database IP Address
- Port
- Database user Name
- Password
Code:
1 Public classMysql_demo1 {2 3 //Create a URL4 Private StaticString url = "jdbc:mysql://localhost:3306";5 6 //CREATE DATABASE Information7 Private StaticString user = "root";8 Private StaticString Possword = "root";9 Ten //The first way One Private Static voidConnection1 ()throwsSQLException { A - //creating a Driver class object -Driver Driver =Newcom.mysql.jdbc.Driver (); the - //receive database information with a collection -Properties Pro =NewProperties (); -Pro.getproperty ("User", user); +Pro.getproperty ("Possword", Possword); - + //Connection AConnection con =driver.connect (URL, pro); at - } - - //The second way - /** - * Connect with Driver Manager in * @throwsSQLException - */ to + Private Static voidConnection2 ()throwsSQLException { - the //Registering Drivers * //through the source code, we know that this method is automatically registered, so there is no need to new a drive object $Driver Driver =Newcom.mysql.jdbc.Driver ();Panax Notoginseng - //connect to a specific database theConnection conn =drivermanager.getconnection (URL, user, possword); + A } the + // third method (recommended) - /** $ * from See driver driver = new Com.mysql.jdbc.Driver (); Source Code $ * As you know, the method inside it is a static method that starts with the load of the class, so we can call it directly with the class, passing the URL field past - * @paramargs - * @throwsException the */ - Private Static voidConnection3 ()throwsException {Wuyi the // pass the URL field directly past, load the static code, and register the driver -Class.forName ("Com.mysql.jdbc.Driver"); Wu - // Connect to a specific database About //drivermanager.getconnection (URL, user, Possword): An attempt was made to establish a connection to a given database URL. $Connection con =drivermanager.getconnection (URL, user, possword); - - } - A Public Static voidMain (string[] args)throwsException { + Connection3 (); the } - $}
JDBC Java database connection 1) Getting Started with JDBC