1. 程式人生 > >java連接Oracle數據庫實現增刪改查並在Navicat中顯示

java連接Oracle數據庫實現增刪改查並在Navicat中顯示

execute etag while args 自動 rest getc from lose

創建TEST表

技術分享圖片

eclipse中的java項目

技術分享圖片

代碼

數據庫方法類 DBUtil:

package util;

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

public class DBUtil {

 // 創建一個數據庫連接
    public static Connection getConnection() 
    {
        Connection connection = null;
    	 String USERNAMR = "system";
         String PASSWORD = "*****";//自己的密碼
         String DRVIER = "oracle.jdbc.OracleDriver";
         String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            Class.forName(DRVIER);
            connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
            System.out.println("成功連接數據庫");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
 
        return connection;
    }
    //關閉資源
    public static void close(Connection connection ) {
		try {
			if (connection != null) {
				connection.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement preparedStatement ) {
		try {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet resultSet ) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

實體類 Model

package test;

public class Model {
    private String id;

    private String username;
    private String password;
    private String name;
    private int age;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public
Model() { } public Model(String id, String username, String password, String name, int age) { super(); this.id = id; this.username = username; this.password = password; this.name = name; this.age = age; } public void update(String username, String password, String name, int
age) { this.username = username; this.password = password; this.name = name; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

測試類 Test

package test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import util.DBUtil;

public class Test {
    public void add(Model model)//添加數據
    {
        Connection connection=DBUtil.getConnection();
        String sql="insert into test(id,username,password,name,age)values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, model.getId());
            preparedStatement.setString(2, model.getUsername());
            preparedStatement.setString(3, model.getPassword());
            preparedStatement.setString(4, model.getName());
            preparedStatement.setInt(5, model.getAge());
            preparedStatement.executeUpdate();
            System.out.println("插入成功");
        } catch (SQLException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }finally{
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }
    public void update(Model model)//修改數據
    {
        Connection connection=DBUtil.getConnection();
        String sql="update test set username=?,password=?,name=?,age=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, model.getUsername());
            preparedStatement.setString(2, model.getPassword());
            preparedStatement.setString(3, model.getName());
            preparedStatement.setInt(4, model.getAge());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }finally{
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }
    public Model load(String id) //查詢數據
    {
        Connection connection = DBUtil.getConnection();
        //準備sql語句
        String sql = "select * from test  where id = ?";
        //創建語句傳輸對象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Model model = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                model = new Model();
                model.setId(id);
                model.setUsername(resultSet.getString("username"));
                model.setPassword(resultSet.getString("password"));
                model.setName(resultSet.getString("name"));
                model.setAge(resultSet.getInt("age"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  model;
    }
    public void delete(String id)//刪除數據
    {
        Connection connection=DBUtil.getConnection();
        String sql="delete from test where id=?";
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            preparedStatement.executeQuery();
            System.out.println("刪除成功");
        } catch (SQLException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }
    
    public static void main(String args[])
    {
        Test test=new Test();
        Model model=new Model("1","123","123","張三",18);
//        test.add(model);
        
        
//        model.update("123","123","張三",28);
//        test.update(model);
//        
//        model=test.load("1");
//        System.out.println("查詢結果————姓名:"+model.getName()+",年齡:"+model.getAge());
//        
        test.delete("1");
    }
}

用Navicat連接Oracle數據庫,因為創建的表自動存放在SYSTEM表空間內,在SYSTEM下邊可以直接找到。

技術分享圖片

技術分享圖片

Navicat中的數據變化:

插入數據。

技術分享圖片

更改後的數據。

技術分享圖片

刪除數據。

技術分享圖片

java連接Oracle數據庫實現增刪改查並在Navicat中顯示