使用步骤:
1.加载驱动程序
Class.forName(driverClass) 每个数据库的驱动都不一样mysql 5.0及以前的版本都用这个
Class.forName("com.mysql.jdbc.Driver")
5.0以后用这个
Class.forName("com.mysql.cj.jdbc.Driver")
其他的请自行百度
2.获得数据库连接:
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database", "username", "password");
database是你要连接的数据库,username是你的数据库用户名,password是密码。
127.0.0.1:3306是你的本地主机地址
3.创建Statement\PreparedStatement对象:
conn.createStatement(); conn.prepareStatement(sql);
具体实现
static Properties properties = new Properties(); //加载驱动 static { InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(resourceAsStream); Class.forName(properties.getProperty("driverClass")); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //获取连接对象 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("username"),properties.getProperty("password")); } public static void close(ResultSet rs, Connection con, PreparedStatement ps){ try { if (rs!=null){ rs.close(); } if (con!=null){ con.close(); } if (ps!=null){ ps.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } public static int executeUpdate(String sql,Object... par){ Connection con=null; PreparedStatement ps=null; try { con=JDBCUtil.getConnection(); ps=con.prepareStatement(sql); if (par !=null && par.length>0){ for (int i = 0; i < par.length; i++) { ps.setObject(i+1,par[i]); } } return ps.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JDBCUtil.close(null,con,ps); } return 0; } //解析Resultset的数据 public static <T> List<T> parseResult(ResultSet rs,Class<T> tClass) throws SQLException, IllegalAccessException, InstantiationException { List<T> list = new ArrayList<>(); if (rs == null){ return null; } //获取到了T类型的数据 while (rs.next()){ //这是一行的数据 T t = tClass.newInstance(); //获取到这个类的所有属性 Field[] fields = tClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); //通过rs.getObject里填field.getName获取列名(前提是属性名和数据库的列名一致) field.set(t,rs.getObject(field.getName())); } list.add(t); } return list; } /* 查询 通过主键查询,传入的参数有多个,不确定,数组类型。然后返回的是一个对象 */ public static <T> List<T> executeQuery(String sql , Class<T> tClass,Object... parms){ Connection con=null; PreparedStatement ps=null; try { con=JDBCUtil.getConnection(); ps=con.prepareStatement(sql); if (parms !=null && parms.length>0){ for (int i = 0; i < parms.length; i++) { ps.setObject(i+1,parms[i]); } } return JDBCUtil.parseResult(ps.executeQuery(),tClass); } catch (SQLException throwables) { throwables.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } finally { JDBCUtil.close(null,con,ps); } return null; }
记得要导入对应数据库的jar包,版本也要对应上。
在项目下创建lib目录,在目录下把jar复制进去。
然后再打开项目结构
选择刚才创建的lib目录 ,然后选择要应用的项目
点击moulde找到对应的项目
这样不管你导入什么jar包,只要再lib目录下都会被扫描到。