SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC。 JDBC全称为:Java Data Base Connectivity(java数据库连接),它主要由接口组成。 组成JDBC的2个包:java.sql javax.sql
开发JDBC应用需要以上2个包的支持外,还需要导入相应JDBC的数据库实现(即数据库驱动)。
需求:编程从user表中读取数据,并打印在命令行窗口中。
(1) 搭建实验环境 :
a、在mysql中创建一个数据库,并创建user表,同时插入数据到表中。
b、新建一个Java工程,并导入数据库驱动。
(2) 编写程序,在程序中加载数据库驱动
a、方式一:DriverManager. registerDriver(Driver driver) b、方式二:Class.forName(“com.mysql.jdbc.Driver”);
Connection conn = DriverManager.getConnection(url,user,pass);
Statement st = conn.createStatement(); ResultSet rs = st.excuteQuery(sql);
dome:
import java.sql.*; import com.sun.org.apache.regexp.internal.recompile; public class dome { /** * @param args */ public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //1.加载驱动 //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); //2获取连接 Connection connection = DriverManager.getConnection(url,username,password); //3.获取向数据库发sql语句的statament对象 Statement stat = connection.createStatement(); //4.向数据库发送sql,获取数据库返回的结果集 ResultSet rsResultSet = stat.executeQuery("select * from user"); //5.从结果集中获取数据while (rsResultSet.next()) {System.out.println("id = "+ rsResultSet.getObject("id"));System.out.println("name = "+ rsResultSet.getObject("name")); System.out.println("password = "+ rsResultSet.getObject("password"));} //6.释放资源(释放链接)rsResultSet.close();stat.close();connection.close();}}
Jdbc程序中的DriverManager用于加载驱动,并创建与数据库的链接,这个API的常用方法:
DriverManager.registerDriver(new Driver()); DriverManager.getConnection(url, user, password);
Class.forName(“com.mysql.jdbc.Driver”);
URL用于标识数据库的位置,程序员通过URL地址告诉JDBC程序连接哪个数据库,
MySql 数据库的URL写法为: jdbc:mysql:[]//localhost:3306/test ?参数名:参数值
常用数据库URL地址的写法:
Oracle:jdbc:oracle:thin:@localhost:1521:skyfin
SqlServer:jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=skyfin
MySql:jdbc:mysql://localhost:3306/skyfin
Mysql的url地址的简写形式: jdbc:mysql://skyfin
常用属性:useUnicode=true&characterEncoding=UTF-8
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly
public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); Statement stat = connection.createStatement(); /* * 执行查找操作 */ ResultSet rsResultSet = stat.executeQuery("select * from user"); while (rsResultSet.next()) { System.out.println("id = "+ rsResultSet.getObject("id")); System.out.println("name = "+ rsResultSet.getObject("name")); System.out.println("password = "+ rsResultSet.getObject("password")); } /* * 执行插入操作 */ String sql = "insert into user(id,name,password) value(6,"+"'staff'"+","+"'staff'"+")"; System.out.println(sql); int statentnum = stat.executeUpdate(sql); if (statentnum>0) { System.out.println("insert ok"); } /* * 执行更新操作 */ sql = "update user set name = 'skstaff' where name = 'staff'"; System.out.println(sql); statentnum = stat.executeUpdate(sql); if (statentnum>0) { System.out.println("update ok"); } /* * 执行删除操作 */ sql = "delete from user where name = 'skstaff'"; System.out.println(sql); statentnum = stat.executeUpdate(sql); if (statentnum>0) { System.out.println("delete ok"); } /* * 资源的释放 */ rsResultSet.close(); stat.close(); connection.close(); }
public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); String sql = "insert into user1(id,name) value(?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); for(int i = 0;i<10000;i++){ preparedStatement.setInt(1, i); preparedStatement.setString(2, "skyfin"+i); preparedStatement.executeUpdate(); } }
public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); String sql = "insert into user1(id,name) value(?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); for(int i = 0;i<10000;i++){ preparedStatement.setInt(1, i); preparedStatement.setString(2, "skyfin"+i); //preparedStatement.executeUpdate(); /* * 使用executeBatch() */ preparedStatement.addBatch(); } //执行批处理 preparedStatement.executeBatch(); }
注意:1. 如果使用了 addBatch() -> executeBatch() 还是很慢,那就得使用到这个参数了rewriteBatchedStatements=true (启动批处理操作)
public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); //关闭自动提交 connection.setAutoCommit(false); String sql = "update user1 set name = ?where id = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); for(int i = 0;i<10000;i++){ preparedStatement.setString(1, "loco"+i); preparedStatement.setInt(2, i); //preparedStatement.executeUpdate(); /* * 使用executeBatch() */ preparedStatement.addBatch(); } //执行批处理 preparedStatement.executeBatch(); preparedStatement.close(); //执行完后手动提交事务 connection.commit(); //打开自动提交 connection.setAutoCommit(true); connection.close(); }
public static void main(String[] args) throws SQLException,ClassNotFoundException{ // TODO Auto-generated method stub String url = "jdbc:mysql://localhost:3306/skyfin"; String username = "root"; String password = "skyfin"; //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); //关闭自动提交 connection.setAutoCommit(false); String sql = "update user1 set name = ?where id = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); for(int i = 0;i<10000;i++){ preparedStatement.setString(1, "skyfin"+i); preparedStatement.setInt(2, i); //preparedStatement.executeUpdate(); /* * 使用executeBatch() */ if (i>0&&i%500 == 0) { preparedStatement.executeBatch(); //如果不想出错后,完全没保留数据,则可以没执行一次提交一次,但得保证数据不会重复 connection.commit(); } preparedStatement.addBatch(); } //执行批处理 preparedStatement.executeBatch(); preparedStatement.close(); //执行完后手动提交事务 connection.commit(); //打开自动提交 connection.setAutoCommit(true); connection.close(); }
public List<customer> getAll(){ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = JdbcUtils.getConnection(); String sql = "select * from customer"; st = conn.prepareStatement(sql); rs = st.executeQuery(); List list = new ArrayList(); while(rs.next()){ Customer c = new Customer(); c.setBirthday(rs.getDate("birthday")); c.setCellphone(rs.getString("cellphone")); c.setDescription(rs.getString("description")); c.setEmail(rs.getString("email")); c.setGender(rs.getString("gender")); c.setId(rs.getString("id")); c.setName(rs.getString("name")); c.setPreference(rs.getString("preference")); c.setType(rs.getString("type")); list.add(c); } return list; }catch (Exception e) { throw new DaoException(e); }finally{ JdbcUtils.release(conn, st, rs); } } </customer>