Package com.wangbingan.jdbc;import java.sql.connection;import java.sql.drivermanager;import java.sql.ResultSet;import java.sql.Statement;public class JdbcUtil {public Static void main (string args[]) throws exception {connection conn = null;// Load Database driver Class.forName ("Com.mysql.jdbc.Driver");// Get database connection try {conn = Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3307/test", "root", "Wangbingan");} catch (exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Database link failed!");} statement There are many ways to Statement stmt = conn.createstatement ();// sqlstring sql = "select * from best "; String sql1= " insert into best (Id,name,phone) VALUES (' 8 ', ' 3 ', ' 3 ')";// Executive Sqlstmt.executeupdate (SQL1); Resultset result = stmt.executequery (SQL);while (Result.next ()) {system.ouT.println (result.getstring ("id"));}}}
Summarize:
IP to be set to 127.0.0.1, the database should be consistent
Database account, password, it must be remembered, not the right to start MySQL account, but in the installation of MySQL, set, generally is root, the password set itself (set, must remember!)
The database port is 3307, not 3306
Resources:
LocalHost cannot access, 127.0.0.01 can access: http://www.webkaka.com/tutorial/server/2013/121611/
JDBC Link mysql database + Execute SQL (MAC)