JDBC访问数据库的步骤,以及三种数据库的驱动和连接字符串

本文详细介绍了使用JDBC进行数据库操作的步骤,包括与Oracle、MySQL和SqlServer等数据库的连接方式,以及通过Java代码实现增删改查的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、JDBC访问数据库的具体步骤:

1.导入数据库的驱动,加载具体的驱动类
2.与数据库建立连接
3.发送sql语句,执行
4.处理结果集(查询)
*

*二、三种数据库的具体驱动:

**
1.Oracle:
驱动jar:ojdbc-x.jar
具体驱动类:Oracle.jdbc.OracleDriver
连接字符串:jdbc:oracle:thin:@localhost:1521:ORCL
2.MySQL:
驱动jar:mysql-connector-java-x.jar
具体驱动类:com.mysql.jdbc.Driver
连接字符串: jdbc:mysql://localhost:3306/数据库实例名
3.SqlServer:
驱动jar:sqljdbc-x.jar
具体驱动类:com.microsoft.sqlserver.jdbc.SQLServerDriver
连接字符串:jdbc:microsoft:sqlserver:localhost:1433;databasename=数据库实例名

以sqlsever为例的jdbc:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.util.concurrent.ExecutionException;


public class JDBCDemo {
	
	private static final String URL = "jdbc:sqlserver://localhost:1433;databasename=Student_info";
	private static final String USERNAME = "xxx";
	private static final String PWD = "xxxxxx";
	/*public static void update() {//增删改
		Statement stmt = null;
		Connection connection = null;
		try{
			//1.导入驱动,加载具体驱动类
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			//2.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//3.发送SQL语句,执行
			stmt = connection.createStatement();
			//String sql = "insert into Student1 values('zs',22)";
			//String sql = "update Student1 set age = 4 where Sname = 'zs'";
			String sql = "delete from Student1 where Sname = 'zs'";
			
			int count = stmt.executeUpdate(sql);
			//4.处理结果集
			if(count>0){
				System.out.println("登陆成功");
			}
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				if(stmt!=null) stmt.close();
				if(connection!=null) connection.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	*/
	public static void query() {
		Statement stmt = null;
		Connection connection = null;
		ResultSet rs = null;
		try{
			//1.1.导入驱动,加载具体驱动类
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			//2.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//3.发送SQL语句,执行
			stmt = connection.createStatement();
			//String sql = "insert into Student1 values('zs',22)";
			//String sql = "update Student1 set age = 4 where Sname = 'zs'";
			String sql = "select Sname,age from Student1";
			
			rs = stmt.executeQuery(sql);
			while(rs.next()){
//				String Sname = rs.getString("Sname");
//				int age = rs.getInt("age");
				String Sname = rs.getString(1);
				int age = rs.getInt(2);
				System.out.println(Sname+"--"+age);
			}
			/*int count = stmt.executeUpdate(sql);
			//4.处理结果集
			if(count>0){
				System.out.println("success");
			}*/
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				if(rs!=null)rs.close();
				if(stmt!=null) stmt.close();
				if(connection!=null) connection.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		//update();
		query();
	}
	
}