java利用jdbc连接数据库

相同的部分拿出

package com.wenhe.total;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DCji {
	
	private static final String PASSWORD = "root123";
	private static final String USERNAME = "root";
	private static final String URL = "jdbc:mysql://localhost:3306/movies?useUnicode=true&characterEncoding=utf8";


	 static{
			// 加载驱动
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(e);
			}
	 } 
	 
		/**
		 *  get Connection
		 * 	获取连接
		 * @return
		 */
		public static Connection getConnection() {
			try {
				return DriverManager.getConnection(URL, USERNAME, PASSWORD);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		

			
		/**
		 *    关闭 结果集
		 *  
		 * @param ResultSet
		 */
		public static void release(ResultSet rs) {
			if (null != rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}

		/**
		 *  Establish Statement
		 * 	关闭 申明
		 * @param Statement
		 */
		public static void release(Statement statement) {
			if (null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}

		/**
		 *  release Connection
		 * 	关闭连接
		 * @param Connection
		 */
		public static void release(Connection connection) {
			if (null != connection) {
				try {
					connection.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
		/**
		 * release ResultSet Statement Connection
		 *
		 * @param rs
		 * @param statement
		 * @param connection
		 */
		public static void release(ResultSet rs, Statement statement, Connection connection) {
			release(rs);
			release(statement);
			release(connection);
		}
		

}

增删改

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.wenhe.total.DCji;

public class Delete {

	public static void main(String[] args) {
		Connection connection = DCji.getConnection();
	
	// 3 创建声明
			Statement statement = null;
			try {
				statement = connection.createStatement();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			// 4 执行SQL
			int count = 0;
			try {
				count = statement.executeUpdate("DELETE FROM t_employee WHERE id = 19");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			System.out.println("影响记录行数: " + count);
			
			//关闭申明,关闭连接
			DCji.release(null, statement, connection);
	}
}

查询

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.wenhe.total.DCji;

public class dada {

	public static void main(String[] args) {
		Connection connection = DCji.getConnection();
		//创建申明
				Statement statement = null ;
				try {
					statement = connection.createStatement();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//执行sql
				ResultSet rs = null ;
				try {
					rs = statement.executeQuery("SELECT id, name, sex, age, dept_id FROM t_employee");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//遍历结果集
				try {
					while(rs.next()) {
						int id = rs.getInt("id");  //以 Java 编程语言中 int 的形式获取此 ResultSet 对象的当前行中指定列的值。 
					String name = rs.getString("name");// Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。
					
					System.out.printf("id = %d, name = %s\n", id, name);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				//关闭结果集,关闭申明,关闭连接
				DCji.release(rs, statement, connection);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值