标签:statement roo password cte java declared lan trace close
DB
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
public class DB {
private static String driver = "com.mysql.jdbc.Driver";//写死
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn = getConnection();
stmt=conn.createStatement();
String sql = "select * from tb_user";
rs = stmt.executeQuery(sql);//执行sql语句 结果保存在rs里
while(rs.next()){//每次只访问一行 必须先调用next 因为如同指向空头节点 return true if the new current row is valid; false if there are no more rows
System.out.println(rs.getString("name"));
}
// 前面的链接和结果让前面的代码继续抛 main throws
sql="insert into tb_user(name,email) values(‘jerry‘,‘123@123‘)";
stmt.executeUpdate(sql);
close(rs);// 是种资源。按顺序关闭 不能一起放一个try里不然抛出异常会导致后面close失败 所以要分别
close(stmt); //内部都可以捕获 异常
close(conn);
//crud
}
public static void close(Object toClose){
//利用反射机制
Class theClass=toClose.getClass();//得到参数的类
try {
Method close = theClass.getMethod("close");//找到这个类叫colse的方法
close.invoke(toClose);//对某个对象做close方法
//规律 改变方法属性时要用方法确定这些方法属性从属于哪个对象
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
public static void close(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//总是要用到的代码 外面的也可以链接这个数据库
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Connection conn;
Class.forName(driver);
String password="";
String url="jdbc:mysql://localhost:3306/chat?useUnicode=true&characterEncoding=UTF-8";
String user="root";
conn = DriverManager.getConnection(url,user,password);
return conn;
}
}
Bank
import java.lang.reflect.Field;
public class Bank {
private int money;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
User
import java.lang.reflect.Field;
public class User {
public static void main(String[] args) {
// TODO Auto-generated method stub
Bank bank = new Bank();
Class<? extends Bank> bankClass = bank.getClass();
Field money;
try {
money = bankClass.getDeclaredField("money");
money.setAccessible(true);//money原本是私有的 改变访问规则
money.setInt(bank, 100);//应该修改字段的对象
System.out.println(bank.getMoney());
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//反射字段 属性名
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
标签:statement roo password cte java declared lan trace close
原文地址:https://www.cnblogs.com/leefree/p/12747374.html