JDBC工具类:
1 package cn.lxr.jdbclx; 2 3 import java.sql.*; 4 5 public class JDBCUtils { 6 private static final String r = "root"; 7 private static final String p = "root"; 8 private static final String url = "jdbc:mysql:///semployee"; 9 private static final String DRIVER = "com.mysql.jdbc.Driver"; 10 //注册驱动。 11 static{ 12 try { 13 Class.forName(DRIVER); 14 } catch (ClassNotFoundException e) { 15 e.printStackTrace(); 16 } 17 } 18 //得到数据库链接。 19 public static Connection getConnection() throws Exception { 20 return DriverManager.getConnection(url,r,p); 21 } 22 //zy1 添加五个工资等级 23 public static int insert (String sql){ //设置为空的变量。 24 Statement stat = null; 25 Connection conn = null; 26 int i = 0; 27 28 try { //获取连接对象 29 conn = getConnection(); //创建一个Statement对象将SQL语句发送到数据库。 30 stat = conn.createStatement(); //执行SQL语句,提交参数给数据库。 31 i = stat.executeUpdate(sql); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 finally { 36 if (i>0){ 37 System.out.println("语句执行成功"); 38 }else{ 39 System.out.println("语句执行失败"); 40 } 41 close(conn,stat); 42 } 43 return i; 44 } 45 46 //zy2 查询表emp的所有数据 47 public static ResultSet seatch(String sql){ 48 ResultSet rs = null; 49 Connection conn = null; 50 Statement stat = null; 51 52 try { 53 conn = getConnection(); 54 stat = conn.createStatement(); 55 rs = stat.executeQuery(sql); 56 57 while(rs.next()){ 58 //uid,uname,Job_id,mgr,state,salary,bonus,Dept_id 59 //获取表里的值和数据类型。 60 String uname = rs.getString("uname"); 61 int job_id = rs.getInt("Job_id"); 62 int mgr = rs.getInt("mgr"); 63 Date sdate = rs.getDate("state"); 64 double salary = rs.getInt("salary"); 65 double bonus = rs.getInt("bonus"); 66 int dept_id = rs.getInt("Dept_id"); //拼接打印。 67 System.out.println( 68 uname+" "+job_id+" "+mgr+" "+sdate+" "+salary+" "+bonus+" "+dept_id 69 ); 70 } 71 } catch (Exception e) { 72 e.printStackTrace(); 73 }finally { //结束释放资源。 74 close(conn,stat,rs); 75 } //返回rs。 76 return rs; 77 } 78 79 80 81 //关闭链接,执行打开的资源。 82 public static void close(Connection conn,Statement stmt){ 83 if (stmt!=null){ 84 try { 85 stmt.close(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 if (conn!=null){ 91 try { 92 conn.close(); 93 } catch (Exception e) { 94 e.printStackTrace(); 95 } 96 } 97 } 98 //关闭所有打开的资源。 99 public static void close(Connection conn, Statement stmt, ResultSet rs){ 100 if (stmt!=null){ 101 try { 102 stmt.close(); 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 }if (conn!=null){ 107 try { 108 conn.close(); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 } 113 if (rs!=null){ 114 try { 115 rs.close(); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 } 120 } 121 122 }
调用:
1 package cn.lxr.jdbclx; 2 3 public class Jdbczy1 { 4 public static void main(String[] args){ 5 insert(); 6 } 7 public static void insert(){ 8 String sql = "insert into salarys(grade,losalary,hisalary) values " + 9 "(1,7000,12000),"+ 10 "(2,12010,14000),"+ 11 "(3,14010,20000),"+ 12 "(4,20010,30000),"+ 13 "(5,30010,66000)"; 14 15 int i = JDBCUtils.insert(sql); 16 System.out.println(i); 17 } 18 public static void seatchs(){ 19 String sql = "select * from emp"; 20 JDBCUtils.seatch(sql); 21 } 22 }