利用JDBC连接数据库的步骤:
1、先要加载驱动程序,方式如下:
Class.forName(driverClass);
//加载MySQL驱动
Class.forName("com.mysql.jdbc.Driver");
//加载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
2、获得数据库的连接
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc", "root", "root");
3、创建Statement\PreparedStatement对象
conn.createStatement(); conn.prepareStatement(sql);