一、下载mysql驱动
http://central.maven.org/maven2/mysql/mysql-connector-java/
package testJdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class test2 { public static void main(String[] args) throws SQLException, ClassNotFoundException { //1、加载驱动器 String JDBCDriver = "com.mysql.jdbc.Driver"; Class.forName(JDBCDriver); //2、连接数据库 String url="jdbc:mysql://127.0.0.1:3306/school"; //链接数据的url String user="root"; //登录数据库的用户名 String password="123456"; //用户的密码 Connection conn = DriverManager.getConnection(url, user, password); Statement st = conn.createStatement(); //一、查询sql String sql="select * from student"; ResultSet rs = st.executeQuery(sql); System.out.println("SId"+" "+"Sname"+" "+"Sage"+" "+"Ssex"); while (rs.next()) { System.out.println(rs.getString("SId")+" "+rs.getString("Sname")+" "+rs.getString("Sage")+" "+rs.getString("Ssex")); } /*//二、修改数据 int update = st.executeUpdate("update student set Sname='xiaolizi' where SId=2"); System.out.println("修改后成功的条数:"+update); */ //三、删除 //st.execute("delete from student where SId='2'"); //四、添加数据 // String sql1 = "insert into student(SId,Sname,Sage,Ssex) values('4','郑','3','2')"; // st.execute(sql1); //关闭连接 conn.close(); st.close(); rs.close(); } }