JAVA连接ACCESS,SQL Server,MySQL,Oracle数据库
import java.sql.*;
/*
* JAVA连接ACCESS,SQL Server,MySQL,Oracle数据库
*
* */
public class JDBC {
public static void main(String[] args)throws Exception {
Connection conn=null;
//====连接ACCESS数据库====
//连接ACCESS(非ODBC连接模式)
//String dbURL ="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=f:/db1.mdb";
//连接ACCESS(ODBC -系统DSN连接模式)
//String dbURL="jdbc:odbc:odbcName";
//conn = DriverManager.getConnection(dbURL);//创建连接对象
//System.out.println("ACCESS数据库连接成功!");
//====连接SQL数据库====
/*
* String dbURL="jdbc:microsoft:sqlserver://localhost:数据库端口;DatabaseName=数据库名";
*
* */
//String dbURL="jdbc:microsoft:sqlserver://localhost:7788;DatabaseName=msdb";
//String userName = "sa";
//String userPwd = "admin";
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//加载驱动
//conn = DriverManager.getConnection(dbURL,userName,userPwd);//创建连接对象
//System.out.println("SQL数据库连接成功!");
//====连接MySQl数据库====
/*
* "jdbc:mysql://localhost/数据库名字?user=用户名&password=密码&useUnicode=true&characterEncoding=gb2312";
*
* */
//String dbURL="jdbc:mysql://localhost/hackwang?user=root&password=hackwang&useUnicode=true&characterEncoding=gb2312";
//Class.forName("org.gjt.mm.mysql.Driver");//加载驱动
//conn = DriverManager.getConnection(dbURL);//创建连接对象
//System.out.println("MySQL数据库连接成功!");
//====连接Oracle数据库====
/*
* String dbURL="jdbc:oracle:thin:@localhost:端口:数据库名";
*
* Oracle的JDBC连接驱动在Oracle安装目录\jdbc\lib\classes12.jar
*
* */
//String dbURL="jdbc:oracle:thin:@localhost:1521:hackwang";
//String userName="scott";
//String userPwd="tiger";
//Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
//conn = DriverManager.getConnection(dbURL,userName,userPwd);//创建连接对象
//System.out.println("Oracle数据库连接成功!");
//创建Statement对象执行SQL语句
Statement stmt = conn.createStatement();
//查询数据库
//ResultSet rs= stmt.executeQuery("select * from Admin");
//while(rs.next())
//{
// String name= rs.getString(2);
// System.out.println(name);
//}
//更新数据库
//String sql="insert into Admin(id,name,sex,age) values('888','张三','男','22')";
//stmt.executeUpdate(sql);
//System.out.println("数据库更新成功!");
//关闭连接
//rs.close();
//stmt.close();
//conn.close();
//System.out.println("数据库关闭!");
}
}