JDBC连接数据库(sqlserver,mysql,oracle)

JDBC连接sqlserver

 

package cn.util;

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

/**
 * JDBC连接sqlserver
 * @author Taylor
 *
 */
public class DbUtil1 {

	/**
	 * sqlserver
	 */
	private String dbUrl="jdbc:sqlserver://localhost:1433;databasename=ATCDW";
	private String dbUserName="sa";
	private String dbPassword="sa123";
	private String jdbcName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
		return con;
	}
	
	/**
	 * 关闭数据库连接
	 * @param con
	 * @throws Exception
	 */
	public void closeCon(Connection con) throws Exception{
		if(con!=null){
			con.close();
		}
	}
	
	/**
	 * 测试数据库连接
	 * @param args
	 */
	public static void main(String[] args) {
		DbUtil1 dbUtil=new DbUtil1();
		Connection con=null;
		try {
			con = dbUtil.getCon();
			Statement st = con.createStatement();
			String sql = "SELECT  * FROM [ATCDW].[dbo].[FlightPlan] F WHERE callsign='KNA8240' ";
			ResultSet list = st.executeQuery(sql);
			
			while(list.next()){
				System.out.println(list.getString(1));
			}
			
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

JDBC连接mysql

 

package cn.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * JDBC连接mysql
 * @author Taylor
 *
 */
public class DbUtil2 {

	/**
	 * mysql
	 */
	private String dbUrl="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8";
	private String dbUserName="root";
	private String dbPassword="1qaz@WSX";
	private String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
		return con;
	}
	
	/**
	 * 关闭数据库连接
	 * @param con
	 * @throws Exception
	 */
	public void closeCon(Connection con) throws Exception{
		if(con!=null){
			con.close();
		}
	}
	
	/**
	 * 测试数据库连接
	 * @param args
	 */
	public static void main(String[] args) {
		DbUtil2 dbUtil=new DbUtil2();
		Connection con=null;
		try {
			con=dbUtil.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

JDBC连接oracle

 

package cn.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * JDBC连接oracle
 * @author Taylor
 *
 */
public class DbUtil3 {

	/**
	 * oracle
	 */
	String dbUrl="jdbc:oracle:thin:@localhost:1521:ORCL";  //shan为数据库名称,1521为连接数据库的默认端口  
	String dbUserName="shan";   //shan为用户名  
	String dbPassword="shan";  //shan为密码  
	String jdbcName="oracle.jdbc.driver.OracleDriver";  
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
		return con;
	}
	
	/**
	 * 关闭数据库连接
	 * @param con
	 * @throws Exception
	 */
	public void closeCon(Connection con) throws Exception{
		if(con!=null){
			con.close();
		}
	}
	
	/**
	 * 测试数据库连接
	 * @param args
	 */
	public static void main(String[] args) {
		DbUtil3 dbUtil=new DbUtil3();
		Connection con=null;
		try {
			con=dbUtil.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

 

 

你可能感兴趣的:(JDBC)