/**
***** author wgy *****
**/
/**
** JDBC-ODBC桥接器方式 供存取标准ODBC数据源
** 如SqlServer、Access......
**/
// url = "jdbc:odbc:数据源名称"; user = "登录用户名"; password = "登录密码";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:Exam","admin","sailor");
/**
** JDBC方式
**/
//1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver"); //加载Mysql的JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver"); //加载Oracle的JDBC驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载SQL Server的JDBC驱动
Class.forName("com.ibm.db2.jdbc.app.DB2Driver"); //加载DB2的JDBC驱动
Class.forName("com.sybase.jdbc.SybDriver"); //加载Sybase的JDBC驱动
//2 定义连接URL 本机 localhost == 127.0.0.1 假设数据库为Exam
String url = "jdbc:mysql://127.0.0.1:3306/Exam"; //连接Mysql数据库
String url = "jdbc:oracle:thin:@ 127.0.0.1:1521:Exam"; //连接Oracle数据库
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Exam"; //连接SQL Server数据库
String url = "jdbc:db2://127.0.0.1:5000/Exam"; //连接DB2数据库
String url = "jdbc:sybase:Tds:127.0.0.1:5007/Exam"; //连接Sybase数据库
//3 建立连接
String userName = "root"; //"sa";
String password = "root"; //"sailor";
Connection conn = DriverManager.getConnection(url,userName,password);
//4 创建Statement对象
Statement stmt = conn.createStatement();
//或创建PreparedStatement对象
//String sql = "insert into exam_users values(?,?,?,?) ";
PreparedStatement pstmt = conn.prepareStatement(sql);
//备注 jtds驱动连接SQLServer
Class.forName("net.sourceforge.jtds.jdbc.Driver"); //加载SQLServer驱动
String url= "jdbc:jtds:sqlserver://localhost:1433/Exam"; //连接SQLServer数据库