public static void main(String[] args) throws Exception { //1注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2建立连接 String url = "jdbc:mysql://xxxx:xxx/xxx"; String usernName = "******"; //登录数据库的账号 String password = "******"; //登录数据库的密码 Connection conn = DriverManager.getConnection(url, usernName, password); //3获取执行sQL语句的对象 Statement statement = conn.createStatement(); //4获取数据库返回的结果 String sql = "delete from test1 where id = " + 1; String sqlUpdate = "update test3 set age = "+ 30 +" where id = " + 2; String sqlInsert = "INSERT INTO test3 VALUES(5,'tomcat','60');"; String query = "SELECT * FROM test3"; //5处理数据集 // int i = statement.executeUpdate(sql); // int s = statement.executeUpdate(sqlUpdate); // int ins = statement.executeUpdate(sqlInsert); ResultSet rs = statement.executeQuery(query); // System.out.println(i + "行受到影响----删除"); // System.out.println(s + "行受到影响----更新"); // System.out.println(ins + "行受到影响----插入"); while (rs.next()){ System.out.println("查询数据:" + rs.getString("name") + ";年龄:" + rs.getString("age")); } //6关闭连接 statement.close(); conn.close(); }