jdbc-connect-database-example:java连接Access Mysql MSSQLServer2000_2016

所需要的jar包可以事先从这里提取
链接: https://pan.baidu.com/s/1khPJyKeeLRSCmRO38yPI7Q 提取码: hnbf
复制这段内容后打开百度网盘手机App,操作更方便哦

java1.8及之后版本连接Access2007之后的accdb文件

注意,要事先配置好相应的访问数据库的帐号和密码,以及对应的可以测试访问的数据库和表,表中的数据

package com.fhzheng.jdbcAccess;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class Example14_1 {

	public static void main(String[] args) {

		Connection con;
		Statement stmt;
		ResultSet rs;
		
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
		} catch (Exception e) {
			System.out.println(e);
		}
		
		try {
			String DbUrl = "e:/eclipse201906/demo.accdb";
			String url = "jdbc:Access:///"+DbUrl;
			con = DriverManager.getConnection(url);
			stmt = con.createStatement();
			String sql = "select number,name,birthday,salary from employee";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String number = rs.getString(1);
				String name = rs.getString(2);
				String birthday = rs.getDate("birthday").toString();
				Double salary = rs.getDouble("salary");
				
				System.out.print(number+" ");
				System.out.print(name+" ");
				System.out.print(birthday+" ");
				System.out.println(salary);
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

}

java加接mysql5.0

注意,要事先配置好相应的访问数据库的帐号和密码,以及对应的可以测试访问的数据库和表,表中的数据

package com.fhzheng.jdbcMysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class Example14_1 {

	public static void main(String[] args) {

		Connection con;
		Statement stmt;
		ResultSet rs;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			System.out.println(e);
		}
		
		try {
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easybuy","root","rootcuit");
			stmt = con.createStatement();
			String sql = "select loginName,userName from easybuy_user";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String loginName = rs.getString(1);
				String userName = rs.getString(2);
				
				System.out.print(loginName+" : ");
				System.out.println(userName);
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

}

java访问sqlserver2000和sqlserver2016数据库

注意,要事先配置好相应的访问数据库的帐号和密码,以及对应的可以测试访问的数据库和表,表中的数据

package com.fhzheng.jdbcMssql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class Example14_1 {

	public static void main(String[] args) {

//		connectSqlServer2000();
		connectSqlServer2016sp1();
		
	}

	private static void connectSqlServer2000() {
		Connection con;
		Statement stmt;
		ResultSet rs;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (Exception e) {
			System.out.println(e);
		}
		
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://10.220.1.37:1433;databaseName=studentdb","root","rootcuit");
			stmt = con.createStatement();
			String sql = "select top 20  学号,姓名,性别,专业,年级,班序号,进校时间 from 学生";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String stuNo = rs.getString(1);
				String stuName = rs.getString(2);
				String stuSex = rs.getString(3);
				Integer stuMajor = rs.getInt(4);
				Integer stuClass = rs.getInt(5);
				Integer stuClassNo = rs.getInt(6);
				Date stuDate = rs.getDate(7);
				
				System.out.print(stuNo+" : ");
				System.out.print(stuName+" ");
				System.out.print(stuSex+" ");
				System.out.print(stuMajor+" ");
				System.out.print(stuClass+" ");
				System.out.print(stuClassNo+" ");
				System.out.print(stuDate+" ");
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	private static void connectSqlServer2016sp1() {
		
		System.out.println("sqlserver2016,core verion:13.0.4");
		Connection con;
		Statement stmt;
		ResultSet rs;
		
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (Exception e) {
			System.out.println(e);
		}
		
		try {
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=studentdb","root","rootcuit");
			stmt = con.createStatement();
			String sql = "select top 10  学号,姓名,性别,专业,年级,班序号,进校时间 from 学生";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String stuNo = rs.getString(1);
				String stuName = rs.getString(2);
				String stuSex = rs.getString(3);
				Integer stuMajor = rs.getInt(4);
				Integer stuClass = rs.getInt(5);
				Integer stuClassNo = rs.getInt(6);
				Date stuDate = rs.getDate(7);
				
				System.out.print(stuNo+" : ");
				System.out.print(stuName+" ");
				System.out.print(stuSex+" ");
				System.out.print(stuMajor+" ");
				System.out.print(stuClass+" ");
				System.out.print(stuClassNo+" ");
				System.out.println(stuDate+" ");
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

}

你可能感兴趣的:(数据库,JDBC,JAVAEE)