ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

jdbc连接步骤

2019-09-01 22:08:56  阅读:195  来源: 互联网

标签:ps jdbc 步骤 empno 连接 set emp catch public


JDBC概述

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。

JDBC使用详解

jdbc就是java连接数据库,在使用前,我们要先让数据库和java建立连接,这里是具体的代码实现。
public class DatabaseUtils {
private static String userName = "root";
private static String password = "123456";
private static String url = "jdbc:mysql:///sutdent_test?useSSL=true";
private static Connection connection;

private DatabaseUtils() {
	
}

static {
	try {
		Class.forName("com.mysql.jdbc.Driver");
		System.out.println("驱动加载成功!");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		System.out.println("驱动加载失败!");
	}
	
}

public static Connection getConnection() {
	try {
		if(connection == null || connection.isClosed()) {
			try {
				System.out.println("连接数据库成功!");
				connection = DriverManager.getConnection(url, userName, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("连接数据库失败!");
			}
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return connection;
}

public static void closeConnection() {
	if(connection != null) {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

}
我们先加载驱动,然后连接数据库,再就是释放资源,我们这里是将这个步骤设置成一个单例模式,然后以后可以直接调用。

设置数据库字段

操作数据库前要把字段转化成java里面的属性,一一对应,方便调用。
import java.sql.Date;

public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Integer sal;
private Integer comm;
private Integer deptno;

public Emp() {
	// TODO Auto-generated constructor stub
}



public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Integer sal, Integer comm,
		Integer deptno) {
	super();
	this.empno = empno;
	this.ename = ename;
	this.job = job;
	this.mgr = mgr;
	this.hiredate = hiredate;
	this.sal = sal;
	this.comm = comm;
	this.deptno = deptno;
}



@Override
public String toString() {
	return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
			+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}

public Integer getEmpno() {
	return empno;
}

public void setEmpno(Integer empno) {
	this.empno = empno;
}

public String getEname() {
	return ename;
}

public void setEname(String ename) {
	this.ename = ename;
}

public String getJob() {
	return job;
}

public void setJob(String job) {
	this.job = job;
}

public Integer getMgr() {
	return mgr;
}

public void setMgr(Integer mgr) {
	this.mgr = mgr;
}

public Date getHiredate() {
	return hiredate;
}

public void setHiredate(Date hiredate) {
	this.hiredate = hiredate;
}

public Integer getSal() {
	return sal;
}

public void setSal(Integer sal) {
	this.sal = sal;
}

public Integer getComm() {
	return comm;
}

public void setComm(Integer comm) {
	this.comm = comm;
}

public Integer getDeptno() {
	return deptno;
}

public void setDeptno(Integer deptno) {
	this.deptno = deptno;
}

}
这里只是对一个单表的操作,如果有其他操作,同理创建即可。

运行sql语句

所有准备工作做完之后,我们就要写sql语句,并且执行命令了。
public class Empdao {
private Connection connection;

public Empdao() {
	this.connection = DatabaseUtils.getConnection();
}

public void add(Emp emp) {
	String sql = "INSERT INTO emp VALUES (?,?,?,?,?,?,?,?)";
	PreparedStatement ps = null;
	try {
		ps = connection.prepareStatement(sql);
		ps.setInt(1, emp.getEmpno());
		ps.setString(2, emp.getEname());
		ps.setString(3, emp.getJob());
		ps.setInt(4, emp.getMgr());
		ps.setDate(5, emp.getHiredate());
		ps.setInt(6, emp.getSal());
		ps.setInt(7, emp.getComm());
		ps.setInt(8, emp.getDeptno());
		ps.execute();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

//删
public void remove() {
	String sql = "DELETE FROM emp WHERE empno = ?";
	PreparedStatement ps = null;
	try {
		System.out.println("请输入您要删除人的empno:");
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		scanner.nextLine();
		ps = connection.prepareStatement(sql);
		ps.setInt(1,t);
		ps.execute();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


//改
public void set() {
	String sql = "UPDATE emp SET sal=? WHERE empno = ?";
	System.out.println("请输入您要修改薪水的人的emptno:");
	Scanner scanner = new Scanner(System.in);
	int empno = scanner.nextInt();
	scanner.nextLine();
	System.out.println("请输入您要修改的薪水sal:");
	int sal = scanner.nextInt();
	scanner.nextLine();
	PreparedStatement ps = null;
	try {
		ps = connection.prepareStatement(sql);
		ps.setInt(1, sal);
		ps.setInt(2, empno);
		ps.execute();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


//查
public Emp get() {
	System.out.println("请输入您要查找的人的empno:");
	Scanner scanner = new Scanner(System.in);
	int empno = scanner.nextInt();
	scanner.nextLine();
	String sql = "SELECT * FROM emp WHERE empno = ?";
	PreparedStatement ps = null;
	ResultSet set = null;
	Emp result = null;
	try {
		ps =connection.prepareStatement(sql);
		ps.setInt(1, empno);
		set = ps.executeQuery();
		while(set.next()) {
			result = new Emp();
			result.setEmpno(set.getInt("empno"));
			result.setEname(set.getString("ename"));
			result.setJob(set.getString("job"));
			result.setMgr(set.getInt("mgr"));
			result.setHiredate(set.getDate("hiredate"));
			result.setSal(set.getInt("sal"));
			result.setComm(set.getInt("comm"));
			result.setDeptno(set.getInt("deptno"));
		}
		
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			ps.close();
			set.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	return result;
}



//模糊查询
public List<Emp> getlike() {
	List<Emp> results = null;
	System.out.println("请输入您要查找的人的姓名关键字:");
	Scanner scanner = new Scanner(System.in);
	String ename = scanner.nextLine();
	String sql = "SELECT * FROM emp WHERE ename like ?";
	PreparedStatement ps = null;
	ResultSet set = null;
	
	try {
		ps =connection.prepareStatement(sql);
		ps.setString(1, "%" + ename + "%");
		set = ps.executeQuery();
		while(set.next()) {
			if (results==null) {
				results = new ArrayList<Emp>();
			}
			Emp emp = new Emp();
			
			emp.setEmpno(set.getInt("empno"));
			emp.setEname(set.getString("ename"));
			emp.setJob(set.getString("job"));
			emp.setMgr(set.getInt("mgr"));
			emp.setHiredate(set.getDate("hiredate"));
			emp.setSal(set.getInt("sal"));
			emp.setComm(set.getInt("comm"));
			emp.setDeptno(set.getInt("deptno"));
			results.add(emp);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			ps.close();
			set.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	return results;
}

}
其中大概模式是一样的,先写sql语句,然后获得句柄,然后执行sql语句,这里写了基本的增删改查,其中我们的查找会用到循环读取,这里和其他的地方不一样,我们将数据保存到一个链表中,然后遍历链表即可在java控制台看见我们表中的数据,最后就是释放资源了。

总结

jdbc中有很多东西需要我们去记忆,里面重要的都是固定写法,再就是我们sql语句的执行。大致的步骤为:

第1步:注冊驱动 (仅仅做一次)
第2步:建立连接(Connection)
第3步:创建运行SQL的语句(Statement)
第4步:运行语句
第5步:处理运行结果(ResultSet)
第6步:然后就是释放资源,先开启的后释放。

标签:ps,jdbc,步骤,empno,连接,set,emp,catch,public
来源: https://blog.csdn.net/Sukura_sk_demo/article/details/100188284

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有