JAVA:JDBC连接SQLserver,MySql,Oracle

一,SQLserver 

package 数据库;

import java.sql.*;

public class SQLserver {
	public static void main(String[] args) {
		try {
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SPJ;","sa", "123456");
			if (connection != null) {
				System.out.print("数据库连接成功");
			} else {
				System.out.print("数据库连接失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

需要将三个jar包导入到项目中:jdk版本1.7以下,sqlserver2012以下


package 数据库;

import java.sql.*;

public class SQLserver {
	public static void main(String[] args) {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SPJ",
					"sa", "123456");
			if (connection != null) {
				System.out.print("数据库连接成功");
			} else {
				System.out.print("数据库连接失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

导入以下包 

jdk1.7以上,sqlserver在2012以上

二,Mysql

8.0

package 数据库;

import java.sql.*;

public class SQL {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver"); 
			Connection connection = DriverManager.getConnection("jdbc:mysql:"
					+ "//127.0.0.1:3306/成绩管理数据库?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT", "root", "123456");
			if(connection!=null){
				System.out.print("数据库连接成功");
			}else{
				System.out.print("数据库连接失败");
			}
		} catch (Exception e) {
	
		}
	}
}

需要导入

5.0

package 数据库;

import java.sql.*;

public class SQL {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver"); 
			Connection connection = DriverManager.getConnection("jdbc:mysql:"
					+ "//127.0.0.1:3306/成绩管理数据库", "root", "123456");
			if(connection!=null){
				System.out.print("数据库连接成功");
			}else{
				System.out.print("数据库连接失败");
			}
		} catch (Exception e) {
	
		}
	}
}

三,Oracle


package Test;

import java.sql.*;

public class Oracle {

	public static void main(String[] args) {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",
					"scott", "tiger");
			if (connection != null) {
				System.out.print("数据库连接成功");
			} else {
				System.out.print("数据库连接失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

需要导入

你可能感兴趣的:(JAVA/JSP)