传统方式访问数据库
1:创建一个Maven web项目
2:修改pom.xml为以下内容
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gx</groupId> <artifactId>SpringData</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringData Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <build> <finalName>SpringData</finalName> </build> </project>
3:创建一个数据库表User,包括id,name,age三个字段,并向其中添加数据
4:创建一个配置文件db.properties,用于存放所有和数据库有关的配置
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3307/test jdbc.username=root jdbc.password=root
5:开发一个JDBC相关工具类com.gx.help.JdbcUtil
package com.gx.help; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 这是一个JDBC工具类 * @author Administrator * *包含的内容 *1:获取Connection *2:释放资源(关闭Connection,Statement(PrepareStatement),ResultSet) */ public class JdbcUtil { /** * 获取Connection * @return 返回所获得的JDBC的Connection * @throws SQLException * @throws ClassNotFoundException * @throws IOException */ public static Connection getConnection() throws SQLException, ClassNotFoundException, IOException{ //从配置文件获取数据 InputStream inputStream = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //驱动程序名 String driver=properties.getProperty("jdbc.driver"); //url指向要访问的数据库名 String url=properties.getProperty("jdbc.url"); //MySQL配置时的用户名和密码 String username=properties.getProperty("jdbc.username"); String password=properties.getProperty("jdbc.password"); Class.forName(driver); Connection conn = DriverManager.getConnection(url,username,password); return conn; } /** * 释放资源(使用Statement的情况下) * @param resultSet * @param statement * @param connection */ public static void close(ResultSet resultSet,Statement statement,Connection connection){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(statement!=null){ try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 释放资源(使用PrepareStatement的情况下) * @param resultSet * @param preparedStatement * @param connection */ public static void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(preparedStatement!=null){ try { preparedStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
5:创建对象模型com.gx.domain.User
package com.gx.domain; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
6:创建Dao的接口com.gx.dao.UserDao
package com.gx.dao; import java.util.List; import com.gx.domain.User; /** * UserDao访问接口 * @author Administrator * */ public interface UserDao { //查询所有用户 public List<User> finaAll(); }
7:创建Dao的实现类com.gx.dao.impl.UserDaoImpl
package com.gx.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.gx.dao.UserDao; import com.gx.domain.User; import com.gx.help.JdbcUtil; /** * UserDao访问接口实现类,这个实现类通过最原始的JDBC的操作实现 * @author Administrator * */ public class UserDaoImpl implements UserDao{ /** * 查询所有学生 */ public List<User> finaAll(){ List<User> users = new ArrayList<User>(); Connection conn=null; PreparedStatement preparedStatement = null; ResultSet result = null; try { String sql = "select * from user"; conn = JdbcUtil.getConnection(); preparedStatement = conn.prepareStatement(sql); result = preparedStatement.executeQuery(); while(result.next()){ User user = new User(); user.setId(result.getInt("id")); user.setName(result.getString("name")); user.setAge(result.getInt("age")); users.add(user); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ JdbcUtil.close(result, preparedStatement, conn); } return users; } }
8:在JDBC相关工具类com.gx.help.JdbcUtil里面添加测试方法
@Test public void testUserQuery(){ UserDao udao = new UserDaoImpl(); List<User> users = udao.finaAll(); for(User user:users){ System.out.println(user.toString()); } }
9:运行测试类
10:添加save方法并测试
UserDao里面的代码
//添加学生 public void save(User user);
UserDaoImpl里面的代码
/** * 添加学生 */ public void save(User user){ Connection conn=null; PreparedStatement preparedStatement = null; ResultSet result = null; try { String sql = "insert into user(id,name,age) value(?,?,?)"; conn = JdbcUtil.getConnection(); preparedStatement = conn.prepareStatement(sql); preparedStatement.setInt(1,user.getId()); preparedStatement.setString(2,user.getName()); preparedStatement.setInt(3,user.getAge()); preparedStatement.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ JdbcUtil.close(result, preparedStatement, conn); } }
JdbcUtilTest里面的代码
@Test public void testUserSave(){ UserDao udao = new UserDaoImpl(); User user = new User(); user.setAge(12); user.setId(6); user.setName("minuobaci"); udao.save(user); }
测试结果
传统方式访问数据库的第二种方式(使用spring框架内置jdbc模板)
时间: 2024-10-17 13:27:05