安装好mysql
下载mysql-connector-java-5.0.8.jar,建立java项目,把其外部引用到项目中
mySql-front中可视化mysql
代码:
import java.sql.*; public class Link { public static void main(String[]args) { try { String url="jdbc:mysql://localhost/test"; String user="root"; String pwd="w111206"; //加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver").newInstance(); //建立到MySQL的连接 Connection conn = DriverManager.getConnection(url,user, pwd); //执行SQL语句 Statement stmt = conn.createStatement();//创建语句对象,用以执行sql语言 ResultSet rs = stmt.executeQuery("select * from teacher"); //处理结果集 while (rs.next()) { String name = rs.getString("id"); System.out.println(name); } rs.close();//关闭数据库 conn.close(); } catch (Exception ex) { System.out.println("Error : " + ex.toString()); } System.out.print("ss"); } }