标签:ace address stack username close rman city ade rac
1 package TommyPackage; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.SQLException; 7 8 public class MyTest { 9 10 private static Connection getConn() { 11 String driver = "com.mysql.jdbc.Driver"; 12 String url = "jdbc:mysql://sh-cdb-kljdeois.sql.tencentcdb.com:63014/yxm"; 13 String username = "root"; 14 String password = "j3s4d5f6"; 15 Connection conn = null; 16 try { 17 Class.forName(driver); //classLoader,加载对应驱动 18 conn = DriverManager.getConnection(url, username, password); 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } catch (SQLException e) { 22 e.printStackTrace(); 23 } 24 return conn; 25 } 26 27 public static void main(String[] args) throws ClassNotFoundException, SQLException { 28 Connection con = getConn(); 29 Person ps = new Person(); 30 31 ps.setLastName("Tommy"); 32 ps.setFirstName("Lorry"); 33 ps.setAddress("电信大楼"); 34 ps.setCity("Shanghai"); 35 36 StringBuilder sb = new StringBuilder(); 37 sb.append("\‘" + ps.getLastName() + "\‘"); 38 sb.append(","); 39 sb.append("\‘" + ps.getFirstName() + "\‘"); 40 sb.append(","); 41 sb.append("\‘" + ps.getAddress() + "\‘"); 42 sb.append(","); 43 sb.append("\‘" + ps.getCity() + "\‘"); 44 45 if (con != null) { 46 System.out.println("mysql 连接成功!"); 47 48 //插入 49 String s = "" + "insert into Persons(LastName,FirstName,Address,City) values(" + sb.toString() + ")"; 50 PreparedStatement pst1 = con.prepareStatement(s); 51 if (pst1.execute()) { 52 System.out.println("插入数据成功!"); 53 } else { 54 System.out.println("插入数据失败!"); 55 } 56 57 //删除 58 String t = "delete from Persons where LastName=‘" + ps.getLastName() + "‘"; 59 PreparedStatement pst2 = con.prepareStatement(t); 60 int i = pst2.executeUpdate(); //i返回删除的记录数 61 System.out.println("删除了: " + i + "条数据!"); 62 63 //修改 64 String Cityname = "London"; 65 String u = "update Persons set LastName=‘" + ps.getLastName() + "‘ where City=‘" + Cityname + "‘"; 66 PreparedStatement pst3 = con.prepareStatement(u); 67 int j = pst3.executeUpdate(); //j返回更新的记录数 68 69 System.out.println("更新了: " + j + "条数据!"); 70 71 //关闭资源 72 pst1.close(); 73 pst2.close(); 74 pst3.close(); 75 con.close(); 76 } 77 } 78 }
标签:ace address stack username close rman city ade rac
原文地址:https://www.cnblogs.com/yangxuming/p/9377117.html