JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问。
目的:不用学习每个数据库的驱动,学习jdbc接口就好了
下面我就举例一个小demo,连接mysql数据库,查询到表中数据:
首先,我的mysql数据库中数据:
用jdbc,我们要引入mysql-connector-java-5.1.41-bin.jar
jdbcDemo1.java
/** * */ package cn.snowing.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import cn.snowing.entity.User; /** * @author: snowing * @date : 2017年4月27日 * jdbc读取数据库信息 */ public class JdbcDemo1 { public static void main(String[] args) throws SQLException, ClassNotFoundException { // 这样也行,默认端口 // String url = "jdbc:mysql:///mydb1"; String url = "jdbc:mysql://localhost:3306/mydb1"; String user = "root"; String password = "1"; Connection conne = null; Statement st = null; ResultSet rs = null; try { // 1.加载驱动注册了两次驱动 // DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // 1.2加载驱动常用方式,只注册了一次驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 conne = DriverManager.getConnection(url, user, password); st = conne.createStatement(); // 4.向数据库发送sql,获取数据库返回的结果集 rs = st.executeQuery("select * from user;"); // 5.从接口集中获取数据 while (rs.next()) { User user1 = new User(); user1.setId(rs.getInt("id")); user1.setUsername(rs.getString("username")); user1.setBirthday(rs.getDate("birthday")); user1.setEntry_date(rs.getDate("entry_date")); user1.setJob(rs.getString("job")); user1.setSalary(rs.getFloat("salary")); user1.setImage(rs.getString("image")); System.out.println(user1.getId()); System.out.println(user1.getUsername()); System.out.println(user1.getBirthday()); System.out.println(user1.getEntry_date()); System.out.println(user1.getJob()); System.out.println(user1.getSalary()); System.out.println(user1.getImage()); } } finally { // 6.释放连接,很重要啊,不要忘记!!这里连接一定要释放,所以放入finally中 if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs = null; } if (st != null) { try { st.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } st = null; } if (conne != null) { try { conne.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } conne = null; } } } }
User.java
/** * */ package cn.snowing.entity; import java.util.Date; /** * @author: snowing * @date : 2017年4月28日 * */ public class User { private int id; private String username; private Date birthday; private Date entry_date; private String job; private float salary; private String resume; private byte image; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getEntry_date() { return entry_date; } public void setEntry_date(Date entry_date) { this.entry_date = entry_date; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public String getResume() { return resume; } public void setResume(String resume) { this.resume = resume; } public byte getImage() { return image; } public void setImage(byte b) { this.image = b; } }
结果: