标签:jdbc访问数据库
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>springdata</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <dependencies> <!-- mysql driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> </project>
package com.mycompany.domain; /** * 学生类 * @author Administrator * */ public class Student { private int sid; private String name; public Student() { super(); } public Student(String name) { super(); this.name = name; } public Student(int sid, String name) { super(); this.sid = sid; this.name = name; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "[sid="+sid+",name="+name+"]"; } }
package com.mycompany.util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * JDBC工具类 * @author Administrator * */ public class JDBCUtil { /** * 获取Connection * @return 返回JDBC的Connection对象 * @throws ClassNotFoundException */ public static Connection getConnection() throws Exception{ //加载属性文件 InputStream inputStream = JDBCUtil.class.getClassLoader() .getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.user"); String password = properties.getProperty("jdbc.password"); String driverClass = properties.getProperty("jdbc.driverClass"); Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); return connection; } /** * 释放资讯 * @param resultSet * @param statement * @param connection */ public static void release(ResultSet resultSet,Statement statement,Connection connection){ if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package com.mycompany.dao; import java.util.List; import com.mycompany.domain.Student; /** * 学生DAO接口 * @author Administrator * */ public interface StudentDao { /** * 查询所有学生 * @return 学生列表 */ public List<Student> query(); /** * 添加学生 * @param student */ public void save(Student student); }
package com.mycompany.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.mycompany.dao.StudentDao; import com.mycompany.domain.Student; import com.mycompany.util.JDBCUtil; public class StudentDaoImpl implements StudentDao { public List<Student> query() { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; List<Student> students = new ArrayList<Student>(); String sql = "select * from student"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ int sid = resultSet.getInt("sid"); String name = resultSet.getString("name"); Student student = new Student(sid,name); students.add(student); } } catch (Exception e) { e.printStackTrace(); } finally{ JDBCUtil.release(resultSet, preparedStatement, connection); } return students; } public void save(Student student) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; List<Student> students = new ArrayList<Student>(); String sql = "insert into student(name) values(?)"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, student.getName()); preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCUtil.release(resultSet, preparedStatement, connection); } } }
jdbc.url=jdbc:mysql:///test jdbc.user=root jdbc.password= jdbc.driverClass=com.mysql.jdbc.Driver
package com.mycompany.dao.impl; import java.util.List; import org.junit.Test; import com.mycompany.dao.StudentDao; import com.mycompany.domain.Student; public class StudentDaoImplTest { @Test public void testQuery(){ StudentDao studentDao = new StudentDaoImpl(); List<Student> students = studentDao.query(); for (Student student : students) { System.out.println(student); } } @Test public void testSave(){ Student student = new Student("test save"); StudentDao studentDao = new StudentDaoImpl(); studentDao.save(student); } }
本文出自 “素颜” 博客,谢绝转载!
标签:jdbc访问数据库
原文地址:http://suyanzhu.blog.51cto.com/8050189/1917017