package demo0131; import java.io.*; public class Demo01 { public static void main(String[] args) { InputStream inputstream = Demo01.class.getClassLoader().getResourceAsStream("haha.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(inputstream)); try{ String text = br.readLine(); System.out.println(text); }catch (IOException e){ e.printStackTrace(); } } }
---------------------------------------------------------------------------------------
package demo0131; public class Person { private String name; private int age; private String phoneNumber; public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } private Person(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age, String phoneNumber) { this.name = name; this.age = age; this.phoneNumber = phoneNumber; } public Person() { } private void show(String name){ System.out.println("竟然可以调用到我,牛掰!!"); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", phoneNumber='" + phoneNumber + '\'' + '}'; } }
@Test public void getClassTest() throws ClassNotFoundException { //获取Class对象,方式一:通过全类名,class的方式获取,一直一个类存在 Class<Person> c1 = Person.class; System.out.println(c1); //方式二:引用.getClass得到类的Class对象 Person person = new Person(); Class c2 = new Person().getClass(); System.out.println(c2); System.out.println(c1 == c2);// //方式三:推荐使用:Class.forName(“全类名”),所谓的全类名就是包名+类名 Class<?> c3 = Class.forName("demo0131.Person"); System.out.println(c3); System.out.println(c1 == c3 && c2 == c3); }
@Test public void getConstructorTest() throws Exception{ //获取类的Class对象 Class c = Class.forName("demo0131.Person"); //获取构造器public Constructor constructor = c.getConstructor(); Object o = constructor.newInstance(); System.out.println(o); System.out.println("-------------------------"); //获取Person类中私有的构造器 Constructor declaredConstructor = c.getDeclaredConstructor(String.class); declaredConstructor.setAccessible(true); Object o1 = declaredConstructor.newInstance("13843838438"); System.out.println(o1); }
@Test public void getMethodTest() throws Exception { Class c = Class.forName("demo0131.Person"); Constructor constructor = c.getConstructor(); Object o = constructor.newInstance(); Method setName = c.getMethod("setName",String.class); setName.invoke(o,"张飞"); System.out.println(o); System.out.println("-------------------------------"); Method declaredMethod = c.getDeclaredMethod("show",String.class); declaredMethod.setAccessible(true); declaredMethod.invoke(o,"哈哈"); System.out.println(o); }
---------------------------------------------------------------------------------------------------------------------------------
package demo0131; import java.sql.*; import java.util.Collection; public class JDBCDemo { public static void main(String[] args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; //1.再项目上新建一个文件夹,lib,导入mysql驱动,并将jar包加入到项目中 try { //2.注册驱动,使用反射第三种方式 Class.forName("com.mysql.cj.jdbc.Driver"); //3.获取连接 String url = "jdbc:mysql://60.205.91.111:3306/userdb"; String username = "userdb"; String password = "userdb"; connection = DriverManager.getConnection(url, username, password); //System.out.println(connection); //4.获取预编译SQL,查询emp表中的数据 String sql = "select * from emp"; preparedStatement = connection.prepareStatement(sql); //5.查询,获取结果表(增删改,添加数据即可)并遍历;查询使用executeQuery //增删改用execute()或者executeUpdate() resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ String ename = resultSet.getString("ename"); String job = resultSet.getString("job"); System.out.println(ename + " / " + job); } }catch (ClassNotFoundException | SQLException e){ e.printStackTrace(); }finally { //6.释放资源Connection,PreparedStatement,ResultSet,释放时需从后往前 //如果connection,preparedStatement,resultSet对象为null JVM GC if (resultSet != null){ try { resultSet.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (preparedStatement != null){ try { preparedStatement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (connection != null){ try { connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } } }
---------------------------------------------------------------------------------------------------------------------------------
package demo0131.util; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; //封装连接与关闭的工具类 public class DBUtils { private static String url = ""; private static String username = ""; private static String password = ""; //静态代码块,只执行一次 static { InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); try { properties.load(inputStream); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){ //如果connection,preparedStatement,resultSet对象为null JVM GC if (resultSet != null){ try { resultSet.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (preparedStatement != null){ try { preparedStatement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (connection != null){ try { connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
package demo0131.util; import org.junit.Test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import static org.junit.Assert.*; public class DBUtilsTest { private static final String INSERT_SQL = "INSERT INTO emp(empno,ename,job) VALUES(?,?,?)"; private static final String UPDATE_SQL = "UPDATE emp SET ename=?,job=? WHERE empno=?"; private static final String DELETE_SQL = "DELETE FROM emp WHERE empno=?"; @Test public void test01() throws SQLException { Connection connection = DBUtils.getConnection(); System.out.println(connection); }
@Test public void insertTest(){ Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DBUtils.getConnection(); preparedStatement = connection.prepareStatement(INSERT_SQL); preparedStatement.setString(1,"2024"); preparedStatement.setString(2,"亮晶晶"); preparedStatement.setString(3,"CEO"); int i = preparedStatement.executeUpdate(); if (i > 0){ System.out.println("插入成功!"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { DBUtils.close(connection,preparedStatement,null); } }
@Test public void updateTest(){ Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DBUtils.getConnection(); preparedStatement = connection.prepareStatement(UPDATE_SQL); preparedStatement.setString(1,"梁静"); preparedStatement.setString(2,"董事长"); preparedStatement.setString(3,"2024"); int i = preparedStatement.executeUpdate(); if (i > 0){ System.out.println("修改成功!"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { DBUtils.close(connection,preparedStatement,null); } }
@Test public void deleteTest(){ Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DBUtils.getConnection(); preparedStatement = connection.prepareStatement(DELETE_SQL); preparedStatement.setString(1,"2024"); boolean b = preparedStatement.execute(); if (b == false){ System.out.println("删除成功!"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { DBUtils.close(connection,preparedStatement,null); } } }