赞
踩
- package com.root;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
-
- public class Handle {
- // 创建一个数据库连接对象
- Connection connection =null;
- // 创建预编译语句对象
- PreparedStatement pstm = null;
- // 创建一个结果集对象
- ResultSet rs = null;
-
- /**
- * 查询数据
- */
- public void SelectData() {
- connection = getConnection();
- String sql = "select * from TB_USER ";
- try {
- pstm = connection.prepareStatement(sql);
- rs = pstm.executeQuery();
- while (rs.next()){
- String id = rs.getString("id");
- String username = rs.getString("username");
- String sex = rs.getString("sex");
- String age = rs.getString("age");
- System.out.println(id+"\t"+username+"\t"+sex+"\t"+age);
- }
- }catch(SQLException e) {
- e.printStackTrace();
- }finally {
- ReleaseResource();
- }
- }
-
- /**
- * 添加用户数据
- */
- public void AddData(int id,String username,int sex,String pf,int age,String school) {
- connection = getConnection();
- String sql="select count(*) from TB_USER" ;
- String sqlStr = "insert into TB_USER values(?,?,?,?,?,?)";
- try {
- pstm = connection.prepareStatement(sql);
- pstm = connection.prepareStatement(sqlStr);
- pstm.setInt(1, id);
- pstm.setString(2, username);
- pstm.setInt(3, sex);
- pstm.setString(4, pf);
- pstm.setInt(5, age);
- pstm.setString(6, school);
- pstm.executeQuery();
- }catch(SQLException e) {
- e.printStackTrace();
- }finally {
- ReleaseResource();
- }
-
-
- }
- /**
- * 向数据库中删除数据
- * @param username:根据姓名删除数据
- */
- public void DeleteData(String stuName) {
- connection = getConnection();
- String sqlStr = "delete from TB_USER where \"id\" = ?";
- System.out.println(stuName);
- try {
-
- pstm = connection.prepareStatement(sqlStr);
- pstm.setString(1, stuName);
- pstm.execute();
- }catch(SQLException e) {
- e.printStackTrace();
- }finally {
- ReleaseResource();
- }
- }
- /*
- *修改数据
- */
- public void UpdateData(String username, String sex, String pf,int age,String school,int id) {
- connection = getConnection();
- String sqlStr = "update TB_USER set \"username\"=?,\"sex\"=?,\"pf\"=?,\"age\"=?,\"school\"=? where \"id\"=?";
- try {
- // 执行插入数据操作
- pstm = connection.prepareStatement(sqlStr);
- pstm.setString(1, username);
- pstm.setString(2, sex);
- pstm.setString(3,pf);
- pstm.setInt(4, age);
- pstm.setString(5, school);
- pstm.setInt(6, id);
- pstm.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- ReleaseResource();
- }
- }
- /*
- * 连接数据库
- */
- public static Connection getConnection() {
- Connection connection = null;
- try {
- System.out.println("开始尝试连接数据库");
- String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
- String user = "root";
- String password = "123456";
- connection = DriverManager.getConnection(url, user, password);
- System.out.println(url);
- System.out.println("用户名:"+user+"\t"+"密码:******");
- System.out.println("数据库连接成功!");
- return connection;
- }
- catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
- // 注意关闭的顺序,最后使用的最先关闭
- public void ReleaseResource() {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (pstm != null) {
- try {
- pstm.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
然后创建一个输出类:
- public class Test {
-
- public static void main(String[] args) {
-
- //创建Handle对象
- Handle hd= new Handle();
- //测试增加数据操作
- //hd.AddData("asd","男","sdfa","dfada",16);
- //测试删除数据操作
- //hd.DeleteData("asd");
- //测试更新数据操作
- hd.UpdateData("asd","女","啊发发","dfada",18);
- //测试查询数据操作
- //hd.SelectData();
-
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。