Driver: Mysql-connector-java-5.1.7-bin.jar.
Examples of the program are:
PackageCommonproject;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJAVAFORJDBC { Public Static voidMain (string[] args)throwsSQLException, classnotfoundexception {String dbdriver= "Com.mysql.jdbc.Driver";//declaring MySQL driverString url = "JDBC:MYSQL://139.129.34.134:3306/DATABASE01";//declaring a data sourceConnection conn =NULL;//declaring a connection to a databaseStatement stmt =NULL;//declaring execution SQL statementsResultSet rs =NULL;//declaring a result setClass.forName (dbdriver);//load the database driver, generally can not execute, the execution of this sentence can be checked in advance whether the JDBC driver exists. conn = drivermanager.getconnection (URL, "username", "password");//connecting to a databasestmt = Conn.createstatement ();//Create a statement objectrs = Stmt.executequery ("SELECT * from Usertable"); Execute Query Statement while(Rs.next ()) {System.out.println ("ID:" +rs.getstring (1) + "Name:" +rs.getstring (2) + "Age:" +rs.getstring (3)); } }}
Java programs connect to MySQL database