java连接数据库

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;
	@Before
	public void setUp() throws Exception {
		runJdbc = new RunJdbc();
	}

	@Test
	public void test() {
		runJdbc.querysql(1,"sq");
		runJdbc.updatesql(2, "wer");
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值