- public static void main(String[] args) {
- Connection conn = null;
- Statement st = null;
-
- try {
- // 1.注册驱动
- String driverName = "com.mysql.jdbc.Driver";
- Class.forName(driverName);
- // DriverManager.registerDriver(new Driver());
- // 2.创建连接
- String url = "jdbc:mysql://localhost:3306/test";
- String username = "root";
- String password = "passw0rd";
- conn = DriverManager.getConnection(url, username,
- password);
- // 3.获得Statement
- st = conn.createStatement();
- // 4.执行SQL
- String sql = "insert into user (usernane, age) " +
- "values ('cc', 22)";
- //String sql2 = "update user set username='zsdsdf' " +
- // "where username='cc'";
-
- int n = st.executeUpdate(sql);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
-
- try
- {
- // 6.关闭资源
- st.close();
- conn.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
-
- }
- }
复制代码 以前的代码 供你参考。
|