JDBC练习四
import java.sql.*;
import java.util.*;
/*使用资源绑定器
*/
public class JDBCTest04 {public static void main(String[] args) {ResourceBundle bundle = ResourceBundle.getBundle("jdbc");String driver = bundle.getString("driver");String url = bundle.getString("url");String user = bundle.getString("user");String password = bundle.getString("password");Connection conn = null;Statement stmt = null;try {Class.forName(driver);conn = DriverManager.getConnection(url,user,password);stmt = conn.createStatement();int count = stmt.executeUpdate("insert into student values(5,'田七')");System.out.println(count == 1? "保存成功":"保存失败");} catch(SQLException e){e.printStackTrace();} catch(ClassNotFoundException e) {e.printStackTrace();} finally {if(conn != null) {try {conn.close();} catch(SQLException e){e.printStackTrace();}}if(stmt != null) {try {stmt.close();} catch(SQLException e){e.printStackTrace();}}}}
}
资源绑定器:
资源绑定器内容为:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=123456