jdbc实现对数据库的具体操作
一、DBUtil类
package com.dbutil;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException {
Connection connection =null;
String driver = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "text";
String pwd = "123";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url,user,pwd);
System.out.println("连接数据库成功");
}catch (Exception e){
System.out.println("连接数据库失败");
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection){
try {
connection.close();
System.out.println("connection关闭成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("connection关闭失败");
}
}
public static void close(PreparedStatement preparedStatement){
try {
preparedStatement.close();
System.out.println("preparedStatement关闭成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("preparedStatement关闭失败");
}
}
public static void close(ResultSet resultSet){
try {
resultSet.close();
System.out.println("resultset关闭成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("resultset关闭失败");
}
}
}
二、Model类
package com.dbutil;
public class Model {
private int id;
private String name;
private String url;
private String des;
public Model(){};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Model(String name, String url, String des) {
this.name = name;
this.url = url;
this.des = des;
}
public Model(int id, String name, String url, String des) {
this.id = id;
this.name = name;
this.url = url;
this.des = des;
}
public void update(int id,String name,String url,String des){
this.id = id;
this.name = name;
this.url = url;
this.des = des;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
@Override
public String toString() {
return "Model{" +
"id=" + id +
", name='" + name + '\'' +
", url='" + url + '\'' +
", des='" + des + '\'' +
'}';
}
}
三、Method
package com.dbutil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Method {
public void add(Model model) throws ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql = "insert into https(id,name,url,des) values(http_seq.nextval,?,?,?)";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, model.getName());
preparedStatement.setString(2, model.getUrl());
preparedStatement.setString(3, model.getDes());
preparedStatement.executeUpdate();
System.out.println("插入成功");
} catch (SQLException e) {
System.out.println("插入失败");
e.printStackTrace();
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public void update(Model model) throws ClassNotFoundException
{
Connection connection = DBUtil.getConnection();
String sql = "update https set name=?,url=?,desc =?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, model.getName());
preparedStatement.setString(2, model.getUrl());
preparedStatement.setString(3, model.getDes());
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("修改失败");
e.printStackTrace();
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public Model select(int id) throws ClassNotFoundException
{
Connection connection = DBUtil.getConnection();
String sql = "select * from https where id = ?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Model model = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
model = new Model();
model.setId(resultSet.getInt( "id"));
model.setName(resultSet.getString("name"));
model.setUrl(resultSet.getString("url"));
model.setDes(resultSet.getString("des"));
}
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
} finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return model;
}
public void delete(int id) throws ClassNotFoundException
{
Connection connection = DBUtil.getConnection();
String sql = "delete from https where id = ?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeQuery();
System.out.println("删除成功");
} catch (SQLException e) {
System.out.println("删除失败");
e.printStackTrace();
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
}
四、Test测试类
package com.dbutil;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
Method method = new Method();
for(int i=0;i<=5;i++) {
System.out.println(method.select(i));
System.out.println("-----------------");
}
}
}
五、数据库的操作