使用jdbc连接oracle数据库

1.导入架包ojdbc14

2.编写工具类

package utils;

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

public class JDBCUtil {
	public static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
	public static final String USER = "system";
	public static final String PASSWORD = "root";
	
	public static Connection getConnection(){
		
		// 加载Oracle驱动
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 连接数据库
		Connection connection = null;
		try {
			connection =  DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

}

3.简单编写三层架构

service.impl

public class UserServiceImpl implements UserService{

	UserMapper um = new UserMapper();
	
	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		String sql = "insert into users values(null,'"+user.getUsername()+"','"+user.getPhone()+"',"+user.getAge()+",'"+user.getAddress()+"',sysdate)";
		return um.addUser(sql);
	}

	@Override
	public List getUserList() {
		// TODO Auto-generated method stub
		String sql = "select * from users";
		return um.getUserList(sql);
	}

}

mapper

package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import domain.User;
import utils.JDBCUtil;

public class UserMapper {
	
	Connection con = null;
	Statement st = null;
	ResultSet rs = null;
	
	public int addUser(String sql){
		int a = 0;
		try {
			con = JDBCUtil.getConnection();
			st  = con.createStatement();
			a = st.executeUpdate(sql);
			con.close();
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}
	
	public List getUserList(String sql){
		List list = new ArrayList();
		try {
			con = JDBCUtil.getConnection();
			st  = con.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPhone(rs.getString("phone"));
				user.setAge(rs.getDouble("age"));
				user.setAddress(rs.getString("address"));
				user.setBirth(rs.getString("birth"));
				list.add(user);
			}
			con.close();
			st.close();
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	

}

测试

public class test {
	
	public static void main(String[] args) {
		
		UserController uc = new UserController();
		
		User user = new User("张思","1313131",1.1,"天津",null);
		int a = uc.addUser(user);
		System.out.println(a);
		
		List list = uc.getUserList();
		for(User use : list){
			System.out.println(use);
		}
		
		
	}

}

你可能感兴趣的:(后端,oracle,java)