Jdbc connection to mysql database
Download the jdbc driver to the official website and place it under the relevant jdk directory, or add the jar file to the project
Package test_mysql; import java. SQL. *; import java. util. set; public class testjdbc {public static Connection getConnection () throws ClassNotFoundException, SQLException {String URL = "jdbc: mysql: // localhost: 3306/employees "; // load the driver Class. forName ("com. mysql. jdbc. driver "); String user =" root "; String passwd =" "; // the default password is null // data of the selected database // DriverManager. // Connection con = DriverManager. getConnection (URL, user, passwd); // Connection con = DriverManager. getConnection (url, user, password); String test_url = ""; // String my_url = "jdbc: mysql: // localhost: 3306; DatabaseName = employees; User = root; password = "; // test_url + = URL + 3306 +"; "+" DatabaseName = employees; "+" User = root; "+" Password = "+ ""; connection con = DriverManager. getConnection (URL, user, passwd); return con;} public static void main (String [] args) {String SQL = "select * fromdepartment S "; Statement statement = null; Connection con = null; ResultSet set = null; try {con = getConnection (); // con. setif (con = null) System. out. println ("con null" contains invalid statement=con.createstatement(;;set=statement.exe cuteQuery (SQL); // int n = set. getFetchSize (); // System. out. println (n); while (set. next () {System. out. println (set. getString (1) + "" + set. getString (2) ;}} catch (ClassNotFoundException e) {// TODO Auto-generated cat Ch blocke. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (con! = Null) try {set. close (); statement. close (); con. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}