Each time write Java link data How to write, this time to do a summary, convenient reference.
1. Download MySQL driver jar package on MySQL
2. Download the driver package
3. Program Directory
4. Procedures
1 Packagemysqltest;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4 Importjava.sql.PreparedStatement;5 ImportJava.sql.ResultSet;6 Public classTest1 {7 Public Static voidMain (string[] args) {8Connection connection=NULL;9PreparedStatement preparedstatement=NULL;TenResultSet resultset=NULL; One Try { AString driverclass= "Com.mysql.jdbc.Driver"; -String url= "Jdbc:mysql://localhost:3306/test"; -String user= "Root"; theString password= "123456"; - Class.forName (driverclass); -connection=drivermanager.getconnection (URL, user, password); -System.out.println ("connect=" +connection); +String sql= "SELECT * from TB1"; -Preparedstatement=connection.preparestatement (SQL); +resultset=preparedstatement.executequery (); A while(Resultset.next ()) { at intId=resultset.getint (1); -String name=resultset.getstring (2); -System.out.println (id+ "....." +name); - } -}Catch(Exception e) { - //Todo:handle Exception in } - to } + -}
5. Interpreting Knowledge points
Jdbc:mysql://localhost:3306/test
3306 is the port number
Test is the database name
There is no table name, and the table name is placed in the SQL statement.
LocalHost is the IP address, as follows
6. Effects
MySQL's first program