热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

Java连接数据库,及增删改查的示例

这篇文章主要介绍了Java连接数据库,及增删改查的示例,帮助大家更好的利用Java处理数据,感兴趣的朋友可以了解下

自定义连接数据库的util类

package com.shuzf.jdbc;

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

public class JDBCUtil {
  // 定义驱动器类的路径
  private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
  // 定义用于连接数据库的URL
  private static final String URL = "jdbc:oracle:thin****l";
  // 定义用于访问数据库的用户名及密码
  private static final String USERNAME = "s****t";
  private static final String PASSWORD = "t***r";

  // 加载驱动器类
  static {
    try {
      Class.forName(DRIVER);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  // 定义获得连接的方法
  public static Connection getConnection() {
    Connection cOnn= null;
    ;
    try {
      cOnn= DriverManager.getConnection(URL, USERNAME, PASSWORD);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return conn;
  }

  // 定义释放数据库资源的方法
  public static void destory(Connection con, Statement stat, ResultSet rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (stat != null) {
      try {
        stat.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (con != null) {
      try {
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

}

基本类

package com.shuzf.jdbc;

public class Student {
  private Integer Id;
  private String Name;
  private String Sex;
  private int Age;
   
  public Student() {
    super();
  }
  public Student(String name, String sex, int age) {
    Id = null;
    Name = name;
    Sex = sex;
    Age = age;
  }
  public Integer getId() {
    return Id;
  }
  public void setId(Integer id) {
    Id = id;
  }
  public String getName() {
    return Name;
  }
  public void setName(String name) {
    Name = name;
  }
  public String getSex() {
    return Sex;
  }
  public void setSex(String sex) {
    Sex = sex;
  }
  public int getAge() {
    return Age;
  }
  public void setAge(int age) {
    Age = age;
  }

}

增删改查

package com.shuzf.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JdbcTest {

  public int insert(Student student) {
    Connection cOnn= JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "insert into students (Name,Sex,Age,Addtime) values(?,?,?,?)";
    try {
      pst = conn.prepareStatement(sql);
      pst.setString(1, student.getName());
      pst.setString(2, student.getSex());
      pst.setInt(3, student.getAge());
      pst.setDate(4, new Date(new java.util.Date().getTime()));
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public int update(Student student) {
    Connection cOnn= JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
    try {
      pst = conn.prepareStatement(sql);
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public int delete(Student student) {
    Connection cOnn= JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "delete from students where Name='" + student.getName() + "'";
    try {
      pst = conn.prepareStatement(sql);
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public ArrayList getStudent(String name) {
    Connection cOnn= JDBCUtil.getConnection();
    PreparedStatement pst = null;
    ResultSet rs = null;
    ArrayList students = new ArrayList();
    String sql = "select * from students where Name='" + name + "'";
    try {
      pst = conn.prepareStatement(sql);
      rs = pst.executeQuery();
      // int count = rs.getMetaData().getColumnCount();// 指示列数目的 int值
      while (rs.next()) {
        Student s = new Student();
        s.setId(rs.getInt("id"));
        s.setName(rs.getString("name"));
        s.setSex(rs.getString("sex"));
        s.setAge(rs.getInt("age"));
        students.add(s);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, rs);
    }
    return students;
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub

  }

}

以上就是Java连接数据库,及增删改查的示例的详细内容,更多关于Java 操作数据库的资料请关注其它相关文章!


推荐阅读
author-avatar
诚实的坏男2502892343
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有