標籤:enc drive update utf8 state rate new tco exec
一、代碼如下
private void insert_Mysql() {// TODO Auto-generated method stubjava.sql.PreparedStatement ps = null;java.sql.Connection cn = null;ResultSet rs = null;try {// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");Class.forName("com.mysql.jdbc.Driver").newInstance();cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");ps = cn.prepareStatement("insert into user(username, password, phone, email) values(?, ?, ?, ?);");ps.setString(1, "wjb");ps.setString(2, "wjb");ps.setString(3, "wjb");ps.setString(4, "wjb");int i= ps.executeUpdate();System.out.println(i);if(i>=1) {System.out.println("新增:成功");}else {System.out.println("新增:失敗");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {ps.close();cn.close();} catch (SQLException e) {e.printStackTrace();}}}
private void insert_Mysql_Two() {// TODO Auto-generated method stubjava.sql.PreparedStatement ps = null;java.sql.Connection cn = null;ResultSet rs = null;try {// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");Class.forName("com.mysql.jdbc.Driver").newInstance();cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");ps = cn.prepareStatement("insert into user(username, password, phone, email) values(‘332‘, ‘332‘, ‘33‘, ‘322‘);");int i= ps.executeUpdate();System.out.println(i);if(i>=1) {System.out.println("新增:成功");}else {System.out.println("新增:失敗");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {ps.close();cn.close();} catch (SQLException e) {e.printStackTrace();}}}
mysql資料庫__Jdbc直接操作__PreparedStatement__insert