JDBC练习四
JDBC練習(xí)四
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();}}}} }資源綁定器:
資源綁定器內(nèi)容為:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb user=root password=123456運(yùn)行結(jié)果:
總結(jié)
- 上一篇: java从键盘上录入信息
- 下一篇: coordinatorlayout_一篇