所需第三方jar包:
mysql-connector-java-5.1.18-bin.jar
ojdbc6.jar
sqljdbc4.jar
百度云盘jdbc 下载地址:http://pan.baidu.com/s/1c0nJbZq
注意:测试数据随便录入几条即可 ^^
0.基本DB类:
package test.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DB { // MySql连接配置 // private static String driver = "com.mysql.jdbc.Driver"; // private static String url = "jdbc:mysql://localhost:3306/test?user=root&password="; // private static String uName="root"; // private static String pwd=""; // Oracle连接配置 // private static String driver = "oracle.jdbc.driver.OracleDriver"; // private static String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // private static String uName="scott"; // private static String pwd="scott"; // SQL Server连接配置 // private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // private static String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=test"; private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static String url = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; private static String user="sa"; private static String password="MATH3759754340"; private Connection conn; private PreparedStatement ps; private ResultSet rs; /** * 获取数据库连接对象 * @return 连接对象 */ public static Connection getConn() { Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); }catch(Exception ex) { System.out.println("数据库驱动 加载异常"); } return conn; } /** * 查询操作 * @param 查询占位语句 * @throws SQLException */ public ResultSet query(String sql,Object[] parms) throws SQLException { // 获取连接 conn = getConn(); // 执行对象 ps = conn.prepareStatement(sql); // 查询条件 if(parms != null) { for (int i = 0; i < parms.length; i++) { ps.setString((i+1),parms[i].toString()); } } // 执行查询,接受结果 rs = ps.executeQuery(); return rs; } /** * 非查询操作 * @param 非查询占位语句 * @throws SQLException */ public int update(String sql,Object[] parms) throws SQLException { // 获取连接 conn = getConn(); // 执行对象 ps = conn.prepareStatement(sql); // 查询条件 if(parms != null) { for (int i = 0; i < parms.length; i++) { ps.setString((i+1),(String) parms[i]);//遍历objs,为预处理的sql语句,一个个的设置条件,注意是从1开始,? ? ? ? 1234 } } // 执行查询,接受结果 int iRet = ps.executeUpdate(); return iRet; } /** * 关闭连接资源 */ public void close() { try { if(conn!=null) conn.close(); if(ps!=null) ps.close(); if(rs!=null) rs.close(); } catch (Exception e) { System.out.println("数据库资源释放,出现了异常"); } } }
1。测试MySQL连接
package test.jdbc; import java.sql.ResultSet; import java.sql.SQLException; public class TestMySQL { public static void main(String[] args) throws SQLException { // String sql = "insert into student(id,name,age) values(4,'wangwu',35)"; // DB db = new DB(); // int i = db.update(sql, null); // db.close(); // if(i>0) // System.out.println("持久化操作成功"); // System.out.println("===================================================="); String sql1 = "select id,name,age from student where id=? "; Object[] prams = new Object[]{"4"}; DB db1 = new DB(); ResultSet rs = db1.query(sql1, prams); while(rs.next()) { System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")); } db1.close(); } public void testQuery() throws SQLException { String sql = "insert into student(id,name,age) values(2,'lisi',40)"; DB db = new DB(); ResultSet rs = db.query(sql, null); while(rs.next()) { System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")); } } } 测试输出结果: ==================================================== 4 wangwu 35
2。测试Oracle连接
代码:
package test.jdbc; import java.sql.ResultSet; import java.sql.SQLException; public class TestOracle { public static void main(String[] args) throws SQLException { String sql1 = "select empno,ename from emp"; DB db1 = new DB(); ResultSet rs = db1.query(sql1, null); System.out.println("empno"+"\t"+"ename"); while(rs.next()) { System.out.println(rs.getInt("empno")+"\t"+rs.getString("ename")); } db1.close(); } } 测试输出结果: empno ename 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD
3。测试SQL Server连接
代码:
package test.jdbc; import java.sql.ResultSet; public class TestSQLServer { public static void main(String[] args) throws Exception { String sql1 = "select id,name,age from student"; DB db1 = new DB(); ResultSet rs = db1.query(sql1, null); System.out.println("id"+"\t"+"name"+"\t"+"age"); while(rs.next()) { System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")); } db1.close(); } } 测试输出结果: 数据库驱动 加载异常 Exception in thread "main" java.lang.NullPointerException at test.jdbc.DB.query(DB.java:63) at test.jdbc.TestSQLServer.main(TestSQLServer.java:12) 初步分析:电脑上的 MSSQLSERVER服务程序确认打开为什么会出现驱动加载异常,难道是连接字符串小细节写错了? 实践推出:原因是 MSSQLSERVER服务的TCP/IP协议没有启用,启用TCP/IP协议,并重新启动MSSQLSERVER服务,就没错了。 此时测试输出结果: id name age 1 张三 20 2 李四 25 3 王五 30