先导入mysql驱动jar包(或oracle
package songyan.jdbc.learn1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import com.mysql.jdbc.Driver; public class Demo1 { //使用jdbc实现查询数据,并显示在控制台 public static void main(String[] args) throws Exception { //注册驱动 DriverManager.registerDriver(new Driver()); //获取链接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day06", "root", "247418"); //得到执行sql语句的对象 Statement sta=conn.createStatement(); //执行sql语句并返回结果 ResultSet rs=sta.executeQuery("select * from users"); //处理结果 while(rs.next()) { System.out.println(rs.getObject(1)+" "+rs.getObject(2)+" "+rs.getObject(3)+" "+rs.getObject(4)+" "+rs.getObject(5)); } //关闭资源 rs.close(); sta.close(); conn.close(); } }
)