java连接数据库

来源:互联网 发布:sem跟seo的比较区别 编辑:程序博客网 时间:2024/06/06 02:14

1. 父类(工具类):


package com.test.dao;import java.sql.*;/** * 数据库工具类 * @author  */public class ToolClass{public static final String DRIVER = "com.mysql.jdbc.Driver";public static final String URL = "jdbc://";public static final String USER = "yz";public static final String PASSWORD = "123";/** * 连接数据库 * @return */public static Connection connection(){Connection connection = null;try {Class.forName(DRIVER);connection = DriverManager.getConnection(URL, USER, PASSWORD);} catch (Exception e) {e.printStackTrace();}return connection;}/** * 查询 * @param sql * @param object * @param connection * @return * @throws Exception */public static ResultSet query(String sql,Object object[],Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) throws Exception{preparedStatement = connection.prepareStatement(sql);for (int i = 0; i < object.length; i++) {preparedStatement.setObject(i+1,object[i]);}resultSet = preparedStatement.executeQuery();return resultSet;}/** * 修改 * @param sql * @param object * @param connection * @return * @throws Exception */public static int update(String sql,Object object[],Connection connection,PreparedStatement preparedStatement) throws Exception{preparedStatement = connection.prepareStatement(sql);for (int i = 0; i < object.length; i++) {preparedStatement.setObject(i+1,object[i]);}int resultSet = preparedStatement.executeUpdate();return resultSet;}/** * 关闭连接 * @param connection * @param preparedStatement * @param resultSet */public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){try {if(connection != null){connection.close();}if(preparedStatement != null){preparedStatement.close();}if(resultSet != null){resultSet.close();}} catch (Exception e) {e.printStackTrace();}}}


2.操作数据库(增删改查):


package com.test.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Timestamp;/** * 操作数据 * @author  * */public class RunJdbc{/** * 查询 * @param id * @param title */public void querysql(int id,String title){String sql = "select * from x where id = ? And title = ?";Object[] object = {id,title};ResultSet resultSet = null;PreparedStatement preparedStatement = null;Connection connection = ToolClass.connection();try {resultSet = ToolClass.query(sql, object, connection, preparedStatement, resultSet);while(resultSet.next()){id = resultSet.getInt("id");Timestamp timestamp = resultSet.getTimestamp("time");System.out.println(timestamp);}} catch (Exception e) {e.printStackTrace();}finally{ToolClass.close(connection, preparedStatement, resultSet);}}/** * 修改 * @param id * @param title * @return */public Boolean updatesql(int id,String title){Boolean flag = false;String sql = "select * from x where id = ? And title = ?";Object[] object = {id,title};PreparedStatement preparedStatement = null;int resultSet = 0;Connection connection = ToolClass.connection();try {connection.setAutoCommit(false);resultSet = ToolClass.update(sql, object, connection, preparedStatement);if(resultSet > 0){flag = true;connection.commit();}} catch (Exception e) {try {connection.rollback();} catch (Exception e1) {e1.printStackTrace();}e.printStackTrace();}finally{ToolClass.close(connection, preparedStatement, null);}return flag;}}


3.Junit测试:


package com.test.dao;import org.junit.Before;import org.junit.Test;public class RunJdbcTest {RunJdbc runJdbc = null;@Beforepublic void setUp() throws Exception {runJdbc = new RunJdbc();}@Testpublic void test() {runJdbc.querysql(1,"sq");runJdbc.updatesql(2, "wer");}}