public class Dome1 {
// main
public static void main(String[] args) {
//ctrl alt l
//读取属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("com\\company\\db");
// ctrl alt v
String driver = bundle.getString("driver");
String user = bundle.getString("user");
String password = bundle.getString("password");
String url = bundle.getString("url");
// 放大 变量 作用域
Connection con = null;
Statement st = null;
ResultSet rs = null;
//注册驱动
try {
Class.forName(driver);
//类名调用 静态方法
//alt enter
//获取连接对象
con = DriverManager.getConnection(url, user, password);
//操作
st = con.createStatement();
//写sql语句
String sql = "insert into user(name,password) values('小明','123456')";
//执行
st.execute(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
//关闭
} finally {
//rs st con
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
public class Dome2 {
// main
public static void main(String[] args) {
//ctrl alt l
//读取属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle(“com\company\db”);
// ctrl alt v
String driver = bundle.getString(“driver”);
String user = bundle.getString(“user”);
String password = bundle.getString(“password”);
String url = bundle.getString(“url”);
// 放大 变量 作用域
Connection con = null;
Statement st = null;
ResultSet rs = null;
//注册驱动
try {
Class.forName(driver);
//类名调用 静态方法
//alt enter
//获取连接对象
con = DriverManager.getConnection(url, user, password);
//操作
st = con.createStatement();
//写sql语句
String sql = "select * from compamy"; //查
rs = st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
} //查
//执行
st.execute(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
//关闭
} finally {
//rs st con
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}