当前位置:   article > 正文

JDBC实现java数据库连接(附完整例子+超详细注解)_java jdbc

java jdbc

本人刚写博客不久,是个新人,望大家能给予一些鼓励。 您的一个赞或者是评论区的一句话都将是对我最大的激励。
在这里插入图片描述

简单一句话先来了解下JDBC
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
说白了就是实现 java和数据库的连接。

附上例子:

先让大家看下我的路径:

看到这么多的类屏幕前的你是不是被吓到了?
在这里插入图片描述
其实也不难。

放代码!
在这里插入图片描述
(以下代码建议按顺序观看)

Dept

package com.etc.entity;
//部门类
public class Dept {
	private int deptNo;  //部门号
	private String deptName;  //部门名
	private String deptAddress;  //部门地址
	//构造方法
	public Dept() {
		super();
	}
	public Dept(String deptName, String deptAddress) {
		super();
		this.deptName = deptName;
		this.deptAddress = deptAddress;
	}
	public Dept(int deptNo, String deptName, String deptAddress) {
		super();
		this.deptNo = deptNo;
		this.deptName = deptName;
		this.deptAddress = deptAddress;
	}
	
	
	
	
 //重写toString方法	
	@Override
	public String toString() {
		return "Dept [deptNo=" + deptNo + ", deptName=" + deptName + ", deptAddress=" + deptAddress + "]";
	}
	
	//get和set访问器
	public int getDeptNo() {
		return deptNo;
	}
	public void setDeptNo(int deptNo) {
		this.deptNo = deptNo;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getDeptAddress() {
		return deptAddress;
	}
	public void setDeptAddress(String deptAddress) {
		this.deptAddress = deptAddress;
	}
	
	
	
	
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

TestAdd

package com.etc.option;
//增方法
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 第一个JDBC程序,向数据库中添加一条记录
 * @author GGBOOM
 *
 */
public class TestAdd {

	public static void main(String[] args) throws ClassNotFoundException {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
		//JDBC需要这个JAR包来完成我们对数据库的访问
		//jar包其实就是很多的字节码文件
		
		//2.连接数据库
		//连接数据库的四个变量
		String driver="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		String url="jdbc:mysql://localhost:3306/myschool";   // 指JDBC连接方式;   jdbc:mysql
		                                                     //指你的本机地址;	 localhost
		                                                     //SQL数据库的端口号;	3306 
		                                                     //要连接的数据库;      myschool 
		String userName="root"; //账号
		String userPwd="root";//密码
		String sql="INSERT INTO dept(deptName,deptAddress) VALUES(?,?)";
		Class.forName(driver);//加载驱动
		Connection conn=null; 	
		PreparedStatement pstmt=null;
		try {
			conn = DriverManager.getConnection(url, userName, userPwd); //可以使用DriverManager获取连接对象
			pstmt=conn.prepareStatement(sql);//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
			pstmt.setString(1,"公关部");
			pstmt.setString(2,"金华");
			//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
			int result=pstmt.executeUpdate();
			if(result>0) {
				System.out.println("插入成功!");
			}else {
				System.out.println("插入失败!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null) {
					pstmt.close();   //关闭操作节省资源
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		
		
		
		
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

TestDel

package com.etc.option;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 第一个JDBC程序,向数据库中添加一条记录
 * @author GGBOOM
 *
 */
public class Testdel {

	public static void main(String[] args) throws ClassNotFoundException {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
		//JDBC需要这个JAR包来完成我们对数据库的访问
		//jar包其实就是很多的字节码文件
		
		//2.连接数据库
		//连接数据库的四个变量
		String driver="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		String url="jdbc:mysql://localhost:3306/myschool";
		String userName="root";
		String userPwd="root";
		String sql="delete from dept where deptNo=?";
		//3.加载驱动
		Class.forName(driver);
		
		//可以使用DriverManager获取连接对象
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			conn = DriverManager.getConnection(url, userName, userPwd);
			//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, 6);
			//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
			int result=pstmt.executeUpdate();
			if(result>0) {
				System.out.println("删除成功!");
			}else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null) {
					pstmt.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		
		
		
		
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

TestUpd

package com.etc.option;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 第一个JDBC程序,向数据库中添加一条记录
 * @author GGBOOM
 *
 */
public class TestUpd {

	public static void main(String[] args) throws ClassNotFoundException {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
		//JDBC需要这个JAR包来完成我们对数据库的访问
		//jar包其实就是很多的字节码文件
		
		//2.连接数据库
		//连接数据库的四个变量
		String driver="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		String url="jdbc:mysql://localhost:3306/myschool";
		String userName="root";
		String userPwd="root";
		String sql="UPDATE DEPT SET deptAddress=? 	WHERE deptno=?";
		//3.加载驱动
		Class.forName(driver);
		
		//可以使用DriverManager获取连接对象
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			conn = DriverManager.getConnection(url, userName, userPwd);
			//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,"金华");
			pstmt.setInt(2, 5);
			//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
			int result=pstmt.executeUpdate();
			if(result>0) {
				System.out.println("修改成功!");
			}else {
				System.out.println("修改失败!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null) {
					pstmt.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		

	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

TestQuery

package com.etc.option;

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

/**
 * 第一个JDBC程序,向数据库中添加一条记录
 * @author GGBOOM
 *
 */
public class TestQuery {

	public static void main(String[] args) throws ClassNotFoundException {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
		//JDBC需要这个JAR包来完成我们对数据库的访问
		//jar包其实就是很多的字节码文件
		
		//2.连接数据库
		//连接数据库的四个变量
		String driver="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		String url="jdbc:mysql://localhost:3306/myschool";
		String userName="root";
		String userPwd="root";
		String sql="SELECT * from dept";
		//3.加载驱动
		Class.forName(driver);
		
		//可以使用DriverManager获取连接对象
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;  //结果集合
		try {
			conn = DriverManager.getConnection(url, userName, userPwd);
			//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				//下面两种凡是的区别,一般使用第二种方式,第一种方式如果我现在调换了列的位置,那么我们要去修改下面的代码
				//第二种方式根据列名来获取值,那么我们不管怎么去修改列的位置,都不会影响到我们的取值
				int deptNo=rs.getInt(1);//通过索引取值
				String deptName=rs.getString("deptName");//通过列名
				String deptAddress=rs.getString("deptAddress");
				System.out.println("deptNo:"+deptNo+" deptName: "+deptName+" deptAddress: "+deptAddress);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
				}
				if(pstmt!=null) {
					pstmt.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		

	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

BaseDao

package com.etc.dao;
 //将增删改查这4个操作的共有语句整理进来
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
		private static final String DRIVER="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		private static final String URL="jdbc:mysql://localhost:3306/myschool";
		private static final String USERNAME="root";
		private static final String USERPWD="root";
		
		//获取连接对象
		public static Connection getConnection() {
			try {
				Class.forName(DRIVER);
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
			Connection conn=null;
			try {
				conn = DriverManager.getConnection(URL, USERNAME, USERPWD);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return conn;
		}
		                                                                      //代表多个Object对象
		public static PreparedStatement setParam(Connection conn,String sql,Object...param) throws SQLException{
			PreparedStatement pstmt=null;
			pstmt=conn.prepareStatement(sql);
			for(int i=0;i<param.length;i++) {
				pstmt.setObject(i+1, param[i]);
			}
			return pstmt;
		}
		
		
		//增删改的通用方法
		public static int exeUpdate(PreparedStatement pstmt) throws SQLException  {
			int result=0;
				//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
			result=pstmt.executeUpdate();
			return result;
			
		}
		
		
		public static void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) {
			try {
				if(pstmt!=null) {
					pstmt.close();
				}
				if(conn!=null) {
					conn.close();
				}
				if(rs!=null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
	
	
	
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

DeptDao

package com.etc.dao;

import java.util.List;

import com.etc.entity.Dept;

/**
 * 部门Dao接口
 * @author GGBOOM
 *
 */
public interface DeptDao {
	/**
	 * 添加
	 * @param dept
	 */
	void deptAdd(Dept dept);
	/**
	 * 根据部门号删除
	 * @param deptNo
	 */
	void deptDel(int deptNo);
	/**
	 * 根据部门号修改
	 * @param dept
	 */
	void deptUpdate(Dept dept);
	/**
	 * 查询全部部门
	 * @return
	 */
	List<Dept> deptQuery();
	
	
	
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

DeptDaoImpl

package com.etc.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.etc.dao.BaseDao;
import com.etc.dao.DeptDao;
import com.etc.entity.Dept;

public class DeptDaoImpl implements DeptDao {

	@Override
	public void deptAdd(Dept dept) {

				String sql="INSERT INTO dept(deptName,deptAddress) VALUES(?,?)";

				
			
				Connection conn=null;
				PreparedStatement pstmt=null;
				try {
					conn = BaseDao.getConnection();
					pstmt=BaseDao.setParam(conn,sql, dept.getDeptName(),dept.getDeptAddress());
					//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
					int result=BaseDao.exeUpdate(pstmt);
					if(result>0) {
						System.out.println("插入成功!");
					}else {
						System.out.println("插入失败!");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					BaseDao.closeAll(conn,pstmt,null);
				}
				
		
	}

	@Override
	public void deptDel(int deptNo) {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
		//JDBC需要这个JAR包来完成我们对数据库的访问
		//jar包其实就是很多的字节码文件
		
		//2.连接数据库
		//连接数据库的四个变量
		String driver="com.mysql.jdbc.Driver";
		//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
		String url="jdbc:mysql://localhost:3306/myschool";
		String userName="root";
		String userPwd="root";
		String sql="delete from dept where deptNo=?";
		//3.加载驱动
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		
		//可以使用DriverManager获取连接对象
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			conn = DriverManager.getConnection(url, userName, userPwd);
			//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, deptNo);
			//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
			int result=pstmt.executeUpdate();
			if(result>0) {
				System.out.println("删除成功!");
			}else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null) {
					pstmt.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		
		
		

	}

	@Override
	public void deptUpdate(Dept dept) {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
				//JDBC需要这个JAR包来完成我们对数据库的访问
				//jar包其实就是很多的字节码文件
				
				//2.连接数据库
				//连接数据库的四个变量
				String driver="com.mysql.jdbc.Driver";
				//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
				String url="jdbc:mysql://localhost:3306/myschool";
				String userName="root";
				String userPwd="root";
				String sql="UPDATE DEPT SET deptAddress=? , deptName=? 	WHERE deptno=?";
				//3.加载驱动
				try {
					Class.forName(driver);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
				
				//可以使用DriverManager获取连接对象
				Connection conn=null;
				PreparedStatement pstmt=null;
				try {
					conn = DriverManager.getConnection(url, userName, userPwd);
					//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
					pstmt=conn.prepareStatement(sql);
					pstmt.setString(1,dept.getDeptAddress());
					pstmt.setString(2,dept.getDeptName());
					pstmt.setInt(3, dept.getDeptNo());
					//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
					int result=pstmt.executeUpdate();
					if(result>0) {
						System.out.println("修改成功!");
					}else {
						System.out.println("修改失败!");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					try {
						if(pstmt!=null) {
							pstmt.close();
						}
						if(conn!=null) {
							conn.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				

		
	}

	@Override
	public List<Dept> deptQuery() {
		//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
				//JDBC需要这个JAR包来完成我们对数据库的访问
				//jar包其实就是很多的字节码文件
				
				//2.连接数据库
				//连接数据库的四个变量
				String driver="com.mysql.jdbc.Driver";
				//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
				String url="jdbc:mysql://localhost:3306/myschool";
				String userName="root";
				String userPwd="root";
				String sql="SELECT * from dept";
				List<Dept> list=new ArrayList<Dept>();
				
				//3.加载驱动
				try {
					Class.forName(driver);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
				
				//可以使用DriverManager获取连接对象
				Connection conn=null;
				PreparedStatement pstmt=null;
				ResultSet rs=null;
				try {
					conn = DriverManager.getConnection(url, userName, userPwd);
					//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
					pstmt=conn.prepareStatement(sql);
					rs=pstmt.executeQuery();
					while(rs.next()) {
						//下面两种凡是的区别,一般使用第二种方式,第一种方式如果我现在调换了列的位置,那么我们要去修改下面的代码
						//第二种方式根据列名来获取值,那么我们不管怎么去修改列的位置,都不会影响到我们的取值
						int deptNo=rs.getInt(1);//通过索引取值
						String deptName=rs.getString("deptName");//通过列名
						String deptAddress=rs.getString("deptAddress");
						System.out.println("deptNo:"+deptNo+" deptName: "+deptName+" deptAddress: "+deptAddress);
						list.add(new Dept(deptNo,deptName,deptAddress));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					try {
						if(rs!=null) {
							rs.close();
						}
						if(pstmt!=null) {
							pstmt.close();
						}
						if(conn!=null) {
							conn.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}

		return list;
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223

TestDept

package com.etc.option;


import com.etc.dao.DeptDao;
import com.etc.dao.impl.DeptDaoImpl;
import com.etc.entity.Dept;

public class TestDept {

		public static void main(String[] args) {
			DeptDao dd=new DeptDaoImpl();//实例了对应的接口
			//新增
			Dept dept=new Dept("公关部","北京");
			dd.deptAdd(dept);
			//修改
//			Dept dept=new Dept(7,"公关部1","上海");
//			dd.deptUpdate(dept);
			//删除
			//dd.deptDel(7);
			//查询
		/*	List<Dept> list=dd.deptQuery();
			for(Dept dept1:list) {
				System.out.println(dept1);
			}*/
			
		}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

不懂的朋友仔细看看我的注释,我这注释写的相当良心了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/847938
推荐阅读
相关标签
  

闽ICP备14008679号